Find intersection of two arrays in JavaScript
This post will discuss how to find the intersection of two arrays in JavaScript. In order words, list out the common values present in each of the arrays.
For example, the intersection of arrays [1,2,3,4]
and [3,4,5]
is [3,4]
.
1. Using Array.prototype.filter()
function
The idea is to check the presence of each element of the first array in the second array. This can be easily done using the indexOf() method with the filter() method in the following manner:
1 2 3 4 5 6 7 8 9 |
var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var common = first.filter(x => second.indexOf(x) !== -1) console.log("The common elements are: " + common); /* Output: The common elements are: 2,3 */ |
To check for the presence of a certain element in an array, you can also use the latest includes() method, which returns a boolean value. This is demonstrated below:
1 2 3 4 5 6 7 8 9 |
var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var common = first.filter(x => second.includes(x)) console.log("The common elements are: " + common); /* Output: The common elements are: 2,3 */ |
Note this solution doesn’t result in unique values in the output.
2. Using Set
Another solution is to convert the array into an ES6 Set and call its has()
method to check for the other array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function intersection(first, second) { var s = new Set(second); return first.filter(item => s.has(item)); }; var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var common = intersection(first, second); console.log("The common elements are: " + common); /* Output: The common elements are: 2,3 */ |
To avoid printing duplicates in the output, you can remove duplicate items from the first array, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function intersection(first, second) { first = new Set(first); second = new Set(second); return [...first].filter(item => second.has(item)); } var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var common = intersection(first, second); console.log("Common elements are: " + common); /* Output: Common elements are: 2,3 */ |
3. Using Lodash/Underscore Library
In case you don’t want to use Set as an intermediate data structure for finding common values, the code can be simplified using underscore or lodash library. The following code example prints the unique values present in given arrays using the intersection() method.
1 2 3 4 5 6 7 8 9 10 11 |
var _ = require('lodash'); // or underscore var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var common = _.intersection(first, second); console.log("The common elements are: " + common); /* Output: The common elements are: 2,3 */ |
4. Using jQuery
With jQuery, you can use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); var $ = require("jquery")(window); var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var secondNotFirst = $(second).not(first); // [ 2, 3, 4, 5 ] - [ 1, 2, 3 ] = [4,5] var common = $(second).not(secondNotFirst); // [ 2, 3, 4, 5 ] - [4,5] = [2,3] console.log("The common elements are:"); for (var i = 0; i < common.length; i++) { console.log(common[i]); } /* Output: The common elements are: 2 3 */ |
The code can be simplified using jQuery filter()
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); var $ = require("jquery")(window); var first = [ 1, 2, 3 ]; var second = [ 2, 3, 4, 5 ]; var common = $(second).filter(first); console.log("The common elements are:"); for (var i = 0; i < common.length; i++) { console.log(common[i]); } /* Output: The common elements are: 2 3 */ |
That’s all about finding the intersection of two arrays in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)