Skip to main content

How to Get the Intersection of Two Arrays in JavaScript

Finding the "intersection" of two arrays—the set of elements that they have in common—is a common task in data manipulation. For example, you might want to find which users are in two different groups, or which products share a common tag.

This guide will teach you the modern, standard, and most performant method for finding the intersection of two arrays by leveraging the power of the Set object.

The most efficient and readable way to find the intersection of two arrays is to first convert one of them into a Set. This allows for a much faster lookup of its elements.

The logic:

  1. Create a Set from one of the arrays (arrayB). A Set provides a highly optimized has() method for checking if an element exists.
  2. Use the Array.prototype.filter() method to iterate over the other array (arrayA).
  3. For each element in arrayA, check if it exists in the setB using the has() method.
  4. The filter() method will return a new array containing only the common elements.

For example, we want to find the common elements between two arrays.

// Problem: Find the elements that are in both arr1 and arr2.
const arr1 = ['a', 'b', 'c', 'c']; // Contains a duplicate 'c'
const arr2 = ['a', 'b', 'd', 'e', 'a'];

Solution: this function first gets the unique common elements.

function getIntersection(arrayA, arrayB) {
const setB = new Set(arrayB);

// Get unique elements from arrayA that are also in setB
const intersection = [...new Set(arrayA)].filter(element => setB.has(element));

return intersection;
}

// Example Usage:
const arr1 = ['a', 'b', 'c', 'c'];
const arr2 = ['a', 'b', 'd', 'e', 'a'];

console.log(getIntersection(arr1, arr2));

Output:

['a', 'b']
note

This method is clean, efficient, and correctly handles duplicate values in the source arrays by returning a new array of unique common elements.

How the solution works:

  1. new Set(arrayB): We create a Set from the second array. This is the key to the solution's performance. Checking setB.has(element) is much faster (O(1) on average) than checking arrayB.includes(element) (O(n)).
  2. [...new Set(arrayA)]: We create a temporary array of unique values from arrayA to iterate over. This ensures our final result doesn't contain duplicates if arrayA had them.
  3. .filter(element => setB.has(element)): The filter method iterates over the unique elements of arrayA and keeps only those that are present in setB.

A Note on an Inefficient Alternative (includes())

You will often see a solution that uses array.includes() inside the filter method. While it is very readable, it should be avoided for large arrays.

The inefficient method:

// ⛔️ This is easy to read but can be very slow.
const intersection = arr1.filter(element => arr2.includes(element));
note

Why is it inefficient? For every single element in arr1, the includes() method has to iterate through the entire arr2. If both arrays have 10,000 elements, this could result in 100,000,000 comparisons. The Set-based approach is dramatically faster in such cases.

How to Get the Intersection of Multiple Arrays

You can extend the core logic to find the common elements among three or more arrays by using reduce().

function getMultiIntersection(...arrays) {
if (arrays.length === 0) {
return [];
}

// Start with the first array and progressively filter it.
return arrays.reduce((accumulator, currentArray) => {
const currentSet = new Set(currentArray);
return accumulator.filter(element => currentSet.has(element));
});
}

// Example Usage:
const arr1 = ['a', 'b', 'c'];
const arr2 = ['a', 'c', 'd'];
const arr3 = ['a', 'e', 'c'];

console.log(getMultiIntersection(arr1, arr2, arr3)); // Output: ['a', 'c']

Conclusion

Finding the intersection of two or more arrays is a common task with a clear, modern, and performant solution.

  • The recommended best practice is to leverage the performance of the Set object.
  • For two arrays, create a Set from one array and then filter the other array against it.
  • This Set-based approach is significantly more performant than solutions that rely on nested loops or array.includes().