JavaScript Program to find Intersection of Unsorted Arrays Last Updated : 05 Apr, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript arrays are versatile structures used to store collections of elements. Oftentimes, these arrays may contain duplicates, and finding the common elements among them, known as the "intersection," becomes necessary. In this article, we'll explore various methods to efficiently find the intersection of unsorted arrays in JavaScript. Below are the approaches to Find the Intersection of Unsorted Arrays in JavaScript: Table of Content Using JavaScript filter() MethodUsing JavaScript Set() MethodUsing JavaScript forEach() MethodUsing JavaScript reduce() MethodUsing JavaScript indexOf() MethodUsing JavaScript filter() MethodThe filter() method creates a new array by filtering elements based on a provided condition. We can use this method to remove duplicates and find the intersection. JavaScript function findIntersection(arr1, arr2) { return arr1.filter(item => arr2.includes(item)); } // Example Usage const arr1 = [3, 1, 5, 2]; const arr2 = [5, 2, 7]; console.log(findIntersection(arr1, arr2)); // Output: [5, 2] Output [5, 2]Using JavaScript Set() MethodThe Set() object in JavaScript allows us to create collections of unique values. We can leverage this to efficiently find the intersection of arrays. JavaScript function findIntersection(arr1, arr2) { const set = new Set(arr1); return arr2.filter(item => set.has(item)); } // Example Usage const arr1 = [3, 1, 5, 2]; const arr2 = [5, 2, 7]; console.log(findIntersection(arr1, arr2)); // Output: [5, 2] Output [5, 2]Using JavaScript forEach() MethodBy iterating over the elements of arrays using the forEach() method, we can identify and retain only the common elements. JavaScript function findIntersection(arr1, arr2) { const intersection = []; arr1.forEach(item => { if (arr2.includes(item) && !intersection.includes(item)) { intersection.push(item); } }); return intersection; } // Example Usage const arr1 = [3, 1, 5, 2]; const arr2 = [5, 2, 7]; console.log(findIntersection(arr1, arr2)); // Output: [5, 2] Output [5, 2]Using JavaScript reduce() MethodThe reduce() method can be utilized to accumulate the common elements while iterating through the arrays. JavaScript function findIntersection(arr1, arr2) { return arr1.reduce((intersection, item) => { if (arr2.includes(item) && !intersection.includes(item)) { intersection.push(item); } return intersection; }, []); } // Example Usage const arr1 = [3, 1, 5, 2]; const arr2 = [5, 2, 7]; console.log(findIntersection(arr1, arr2)); // Output: [5, 2] Output [5, 2]Using JavaScript indexOf() MethodWe can iterate through the arrays and use the indexOf() method to find common elements efficiently. JavaScript function findIntersection(arr1, arr2) { const intersection = []; arr1.forEach(item => { if (arr2.indexOf(item) !== -1 && !intersection.includes(item)) { intersection.push(item); } }); return intersection; } // Example Usage const arr1 = [3, 1, 5, 2]; const arr2 = [5, 2, 7]; console.log(findIntersection(arr1, arr2)); // Output: [5, 2] Output [5, 2] Comment More infoAdvertise with us Next Article JavaScript Program to find Intersection of Unsorted Arrays isandeep2183 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Find Union and Intersection of Two Unsorted Arrays In this article, we will learn how to find the Union and Intersection of two arrays. When an array contains all the elements that are present in both of the arrays, it is called a union. On the other hand, if an array has only those elements that are common in both of the arrays, then it is called a 13 min read JavaScript Program to Find if Arrays are Disjoint or Not Given two arrays, Our task is to check if the given two arrays are disjoint or not. Disjoint arrays in JavaScript are arrays that have no elements in common Input: arr1[] = [12, 34, 11, 9, 3] arr2[] = [2, 1, 3, 5]Output: Not Disjoint3 is common in two sets.Input: arr1[] = [12, 34, 11, 9, 3] arr2[] = 3 min read JavaScript Program to Find the Distance Value between Two Arrays We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= 3 min read JavaScript Program to Find Common Elements Between Two Sorted Arrays using Binary Search Given two sorted arrays, our task is to find common elements in two sorted arrays then the program should return an array that contains all the elements that are common to the two arrays. The arrays can contain duplicate elements, that is, values at one index in an array can be equal to the values a 3 min read PHP Find Union & Intersection of Two Unsorted Arrays Given two unsorted arrays, i.e. arr1, and arr2, the task is to find the union and intersection of two unsorted arrays in PHP. Union and intersection of two arrays are common operations in data manipulation and analysis. The union of two arrays includes all the unique elements from both arrays, while 3 min read Javascript Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6Second linked list be 2- 3 min read PHP | Find Intersection of two arrays You are given two arrays of n-elements each. You have to find all the common elements of both elements, without using any loop in php and print the resulting array of common elements. Example: Input : array1[] = {3, 5, 2, 7, 9}, array2[] = {4, 3, 2, 7, 8} Output : array ( [0] => 3, [1] => 2, [ 2 min read How to Find Union and Intersection of Two Unsorted Arrays in PHP? Given two unsorted arrays representing two sets (elements in every array are distinct), find the union and intersection of two arrays in PHP. Example: Input: arr1[] = {7, 1, 5, 2, 3, 6} , arr2[] = {3, 8, 6, 20, 7} Output: Union {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}These are the fol 4 min read Javascript Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists havi 7 min read Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0 5 min read Like