0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Common Elements in Two Arrays

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

Examples

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

How to Think About It

To find common elements, look at each item in the first array and check if it is also in the second array. Collect all items that appear in both arrays.
📐

Algorithm

1
Get the first array and the second array as input.
2
For each element in the first array, check if it exists in the second array.
3
If it does, add it to a new list of common elements.
4
Return the list of common elements.
💻

Code

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

const commonElements = arr1.filter(element => arr2.includes(element));
console.log(commonElements);
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 is in [2, 3, 4] → No

2

Check second element

Check if 2 is in [2, 3, 4] → Yes, add 2

3

Check third element

Check if 3 is in [2, 3, 4] → Yes, add 3

4

Return result

Common elements are [2, 3]

Element from arr1Is in arr2?Common Elements So Far
1No[]
2Yes[2]
3Yes[2, 3]
💡

Why This Works

Step 1: Filtering the first array

We use filter to go through each element of the first array and decide if it should be kept.

Step 2: Checking presence in second array

For each element, includes checks if it exists in the second array, returning true or false.

Step 3: Collecting common elements

Only elements that return true from includes are kept, forming the array of common elements.

🔄

Alternative Approaches

Using Set and filter
javascript
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];
const set2 = new Set(arr2);
const common = arr1.filter(x => set2.has(x));
console.log(common);
Using a Set for the second array improves lookup speed, especially for large arrays.
Using nested loops
javascript
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];
const common = [];
for (let i = 0; i < arr1.length; i++) {
  for (let j = 0; j < arr2.length; j++) {
    if (arr1[i] === arr2[j]) {
      common.push(arr1[i]);
      break;
    }
  }
}
console.log(common);
This method is simple but slower for big arrays because it checks every pair.

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

Time Complexity

The main method 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 output array stores common elements, which can be at most the size of the smaller array, so O(min(n, m)) space.

Which Approach is Fastest?

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

ApproachTimeSpaceBest For
filter + includesO(n * m)O(min(n, m))Small arrays or simple code
filter + SetO(n)O(m)Large arrays needing speed
Nested loopsO(n * m)O(min(n, m))Very small arrays or educational purposes
💡
Use a Set for faster lookups when working with large arrays.
⚠️
Forgetting that includes checks for exact matches and may miss duplicates or different types.