DEV Community

Avnish
Avnish

Posted on

How to Find Common Elements in Two Arrays in JavaScript

In JavaScript, there are several ways to determine whether two arrays share common elements. Each approach varies in terms of simplicity, readability, and efficiency. This article covers five effective methods to achieve this, ranging from simple loops to more advanced techniques using modern JavaScript features like Set and array methods.


1. Using Loops – Simple but Less Efficient

A straightforward way to find common elements is by using nested loops to compare each element in one array with every element in the other. This approach works well for small arrays but is less efficient for larger datasets due to its time complexity of (O(n^2)).

const a1 = [1, 2, 3];
const a2 = [4, 5, 3];

let hasCommonItem = false;
for (let i = 0; i < a1.length; i++) {
    for (let j = 0; j < a2.length; j++) {
        if (a1[i] === a2[j]) {
            hasCommonItem = true;
            break;
        }
    }
    if (hasCommonItem) break;
}

console.log(hasCommonItem); // Output: true
Enter fullscreen mode Exit fullscreen mode

2. Using some() and includes()

The some() method, combined with includes(), offers a concise way to check if any item in one array exists in the other. This approach is easy to read and performs better than nested loops.

const a1 = [1, 2, 3];
const a2 = [4, 5, 3];

const hasCommonItem = a1.some(item => a2.includes(item));
console.log(hasCommonItem); // Output: true
Enter fullscreen mode Exit fullscreen mode

3. Using Set and some() for Efficiency

For larger arrays, converting one of the arrays to a Set can significantly reduce the time complexity since Set provides constant-time lookups. Use some() to check if any element in the first array exists in the Set.

const a1 = [1, 2, 3];
const a2 = [4, 5, 3];

const set = new Set(a2);
const hasCommonItem = a1.some(item => set.has(item));
console.log(hasCommonItem); // Output: true
Enter fullscreen mode Exit fullscreen mode

4. Using filter() to Find Common Items

The filter() method can be used to extract an array of common elements. If the result is non-empty, the arrays share common items.

const a1 = [1, 2, 3];
const a2 = [4, 3, 5];

const commonItems = a1.filter(item => a2.includes(item));
console.log(commonItems); // Output: [3]
console.log(commonItems.length > 0); // Output: true
Enter fullscreen mode Exit fullscreen mode

5. Using Set Intersection for Unique Common Elements

For an efficient and unique solution, convert both arrays into Set objects. Then use filter() to create an intersection of the two sets.

const a1 = [1, 2, 3];
const a2 = [3, 4, 5];

const intersection = new Set(a1.filter(item => new Set(a2).has(item)));
console.log(intersection); // Output: Set { 3 }
console.log(intersection.size > 0); // Output: true
Enter fullscreen mode Exit fullscreen mode

Why Finding Common Items Between Arrays Matters

Identifying common elements between arrays is a frequent requirement in programming, particularly when working with datasets. Some common use cases include:

  • Filtering shared data between two collections.
  • Validating user input against a predefined list of values.
  • Optimizing search algorithms or queries.

Choosing the right method depends on the size of your arrays and the specific requirements of your task. For smaller arrays, simpler methods like nested loops or some() work fine. For larger datasets, leveraging Set can significantly improve performance.


By understanding these techniques, you’ll be better equipped to handle scenarios requiring comparisons between arrays in JavaScript. Pick the method that best suits your needs and write efficient, clean code!

Top comments (0)