0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Intersection of Two Arrays

Use const intersection = arr1.filter(value => arr2.includes(value)); to find the common elements between two arrays in JavaScript.
📋

Examples

Input[1, 2, 3], [2, 3, 4]
Output[2, 3]
Input["apple", "banana", "cherry"], ["banana", "dragonfruit", "apple"]
Output["banana", "apple"]
Input[5, 6, 7], [8, 9, 10]
Output[]
🧠

How to Think About It

To find the intersection, look at each element in the first array and check if it also exists in the second array. Collect all such elements that appear in both arrays. This gives the common elements or intersection.
📐

Algorithm

1
Get the two input arrays.
2
For each element in the first array, check if it is present in the second array.
3
If yes, add that element to a new result array.
4
After checking all elements, return the result array containing common elements.
💻

Code

javascript
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];

const intersection = arr1.filter(value => arr2.includes(value));
console.log(intersection);
Output
[2, 3]
🔍

Dry Run

Let's trace the example arrays [1, 2, 3] and [2, 3, 4] through the code.

1

Start with first element

Check if 1 (from arr1) is in arr2 [2, 3, 4] → No

2

Check second element

Check if 2 (from arr1) is in arr2 [2, 3, 4] → Yes, add 2 to result

3

Check third element

Check if 3 (from arr1) is in arr2 [2, 3, 4] → Yes, add 3 to result

4

Return result

Result array is [2, 3]

Element from arr1Is in arr2?Result array
1No[]
2Yes[2]
3Yes[2, 3]
💡

Why This Works

Step 1: Filtering elements

The filter method goes through each element of the first array and keeps only those that pass the test.

Step 2: Checking presence

The test uses includes to see if the current element exists in the second array.

Step 3: Building intersection

Elements passing the test are collected into a new array, which is the intersection.

🔄

Alternative Approaches

Using Set and filter
javascript
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];
const set2 = new Set(arr2);
const intersection = arr1.filter(value => set2.has(value));
console.log(intersection);
Using a Set for the second array improves lookup speed, making it faster for large arrays.
Using for loop and includes
javascript
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];
const intersection = [];
for (const val of arr1) {
  if (arr2.includes(val)) {
    intersection.push(val);
  }
}
console.log(intersection);
This older approach is more verbose but easy to understand for beginners.
Using reduce and includes
javascript
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];
const intersection = arr1.reduce((acc, val) => {
  if (arr2.includes(val)) acc.push(val);
  return acc;
}, []);
console.log(intersection);
Using reduce is functional style but slightly more complex to read.

Complexity: O(n * m) time, O(min(n, m)) space

Time Complexity

The main approach uses filter on the first array (size n) and includes on the second array (size m), resulting in O(n * m) time.

Space Complexity

The result array stores at most the smaller array's size elements, so space is O(min(n, m)).

Which Approach is Fastest?

Using a Set for the second array reduces lookup to O(1), improving time to O(n), which is faster for large arrays.

ApproachTimeSpaceBest For
filter + includesO(n * m)O(min(n, m))Small arrays or simple code
filter + Set.hasO(n)O(m)Large arrays needing speed
for loop + includesO(n * m)O(min(n, m))Beginners learning loops
reduce + includesO(n * m)O(min(n, m))Functional programming style
💡
Use a Set for the second array to speed up lookups when arrays are large.
⚠️
Forgetting that includes checks for exact matches and does not handle nested arrays or objects.