Find common elements of two arrays in JavaScript

Updated

The objective is to find the common elements of the arrays, which means if there are two arrays given as

A1 = [5, 6, 2, 3, 4]
A2 = [4, 8, 9, 1, 3]

The common elements will be [4,3]

This can be done by using simple looping on arrays, there can be several techniques and each will have its own time and space complexity, some are described below.

Nested Loops :

For every element in Array 1, check whether it exists in Array 2 or not, if YES then push it to the common elements array otherwise do nothing and proceed to the next iteration.

Program :

//Initialize the Array 1 and Array 2
let arr1 = [2, 3, 1, 6, 7, 9];
let arr2 = [11, 13, 5, 3, 6, 4];

//Initalize empty array of common elements
let commonElements = [];

//Run the outer loop till the array end
for(let i = 0 ; i < arr1.length ; i++)
{
    for(let j = 0 ; j < arr2.length ; j++)
    {
        
        

        //If any element found, then push and break
        if(arr1[i]==arr2[j])
        {
            commonElements.push(arr1[i]);
            break;
        }
    }
}

console.log("Common Elements are:- ", commonElements);

Output :

Common Elements are:- [3, 6]

Analysis :

Time Complexity : O(n2)

Space Complexity : O(1)

Additional Point:- We can sort Array 2 so that every time for searching an element of Array 1. We can check it in logn iteration with binary search instead of n iteration with linear search. 

Hashing :

This technique will take constant time to determine whether an element of Array 1 is present in Array 2 or not. We are going to store the elements of Array 2 as key in the hashmap and true as a value. And while iterating on Array 1 we will check whether the hashmap contains this element. It can be done by using has() method of Map Prototype, which returns true if the provided element is present as a key in the Hashmap.

Program :

//Initialize the Array 1 and Array 2
let arr1 = [2, 3, 1, 6, 7, 9];
let arr2 = [11, 13, 5, 3, 6, 4];

//Initalize empty array of common elements
let commonElements = [];

//Initalize a New hashmap with the Map constructor
let myHash = new Map();

//Create hashmap
//Run the loop for array 2 and set the key as element of array 2 and value as true
for(let i = 0 ; i < arr2.length ; i++)
{
  myHash.set(arr2[i], true);
}

for(let i = 0 ; i < arr1.length ; i++)
{
  //If hash contains any element of arr1 then it must be common
  if(myHash.has(arr1[i])) 
    commonElements.push(arr1[i]);
}

console.log("Common Elements are:- ", commonElements);

Output :

Common Elements are:- [3, 6]

Analysis :

Time Complexity : O(n)

Space Complexity : O(n)

Sorted Arrays :

Another thing we can do is to sort both arrays and then use two pointers one for Array 1 and the other for Array 2. Both pointers will initially be at the first index and at the end of the algorithm they will be on the last index after some increments.

The decision of increment will be based on whether the element corresponding to the pointer is greater or smaller than the other. If the number corresponding to pointer i is lesser than the number on pointer j. The pointer i will be incremented and vice versa.

If both elements are equal means we have found the common element and in this case, both pointers will be incremented and we’ll store the element.

Two sorted arrays with pointers i and j stepping right, with the common elements 3 and 6 marked in orange.

Program :

//Initialize the Array 1 and Array 2
let arr1 = [2, 3, 1, 6, 7, 9];
let arr2 = [11, 13, 5, 3, 6, 4];

//Initalize empty array of common elements
let commonElements = [];

//We will have to pass the compare function to sort method of array,
//because by default sort method doesn't works with numbers
function compare(a, b) {

    //If we are returning negative value from the compare fn,
    //means we want to keep the a before b
    if (a<b) {
      return -1;
    }
    
    //Vice versa
    if (a>b) {
      return 1;
    }

    //0 return value means that we are going to place 
    //the a and b in same order as they appeared
    return 0;
}

//Sort both array
arr1.sort(compare);
arr2.sort(compare);

//Declare two pointers
let i = 0, j = 0;

//Run loop till one of the pointers reaches to end of array
while(i<arr1.length && j<arr2.length){
        if(arr1[i]==arr2[j]){
            commonElements.push(arr1[i]);
            i++;
            j++;
        }
        
        else if(arr1[i]>arr2[j])
        {
            j++;
        }

        else if(arr1[i]<arr2[j])
        {
            i++;
        }
}
console.log("Common Elements are:- ", commonElements);

Output :

Common Elements are:- [3, 6]

Analysis :

Time Complexity : O(nlogn)

Space Complexity : O(1)

Comment
Mithlesh Upadhyay Published 25 Nov 2026 · 4 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials