JavaScript Program to find Intersection of Unsorted Arrays

JavaScript arrays are versatile structures used to store collections of elements. Oftentimes, these arrays may contain duplicates, and finding the common elements among them, known as the “intersection,” becomes necessary. In this article, we’ll explore various methods to efficiently find the intersection of unsorted arrays in JavaScript.

Below are the approaches to Find the Intersection of Unsorted Arrays in JavaScript:

Table of Content

  • Using JavaScript filter() Method
  • Using JavaScript Set() Method
  • Using JavaScript forEach() Method
  • Using JavaScript reduce() Method
  • Using JavaScript indexOf() Method

Using JavaScript filter() Method

The filter() method creates a new array by filtering elements based on a provided condition. We can use this method to remove duplicates and find the intersection.

JavaScript
function findIntersection(arr1, arr2) {
    return arr1.filter(item => arr2.includes(item));
}

// Example Usage
const arr1 = [3, 1, 5, 2];
const arr2 = [5, 2, 7];
console.log(findIntersection(arr1, arr2)); // Output: [5, 2]

Output

[5, 2]

Using JavaScript Set() Method

The Set() object in JavaScript allows us to create collections of unique values. We can leverage this to efficiently find the intersection of arrays.

JavaScript
function findIntersection(arr1, arr2) {
    const set = new Set(arr1);
    return arr2.filter(item => set.has(item));
}

// Example Usage
const arr1 = [3, 1, 5, 2];
const arr2 = [5, 2, 7];
console.log(findIntersection(arr1, arr2)); // Output: [5, 2]

Output

[5, 2]

Using JavaScript forEach() Method

By iterating over the elements of arrays using the forEach() method, we can identify and retain only the common elements.

JavaScript
function findIntersection(arr1, arr2) {
    const intersection = [];
    arr1.forEach(item => {
        if (arr2.includes(item) && !intersection.includes(item)) {
            intersection.push(item);
        }
    });
    return intersection;
}

// Example Usage
const arr1 = [3, 1, 5, 2];
const arr2 = [5, 2, 7];
console.log(findIntersection(arr1, arr2)); // Output: [5, 2]

Output

[5, 2]

Using JavaScript reduce() Method

The reduce() method can be utilized to accumulate the common elements while iterating through the arrays.

JavaScript
function findIntersection(arr1, arr2) {
    return arr1.reduce((intersection, item) => {
        if (arr2.includes(item) && !intersection.includes(item)) {
            intersection.push(item);
        }
        return intersection;
    }, []);
}

// Example Usage
const arr1 = [3, 1, 5, 2];
const arr2 = [5, 2, 7];
console.log(findIntersection(arr1, arr2)); // Output: [5, 2]

Output

[5, 2]

Using JavaScript indexOf() Method

We can iterate through the arrays and use the indexOf() method to find common elements efficiently.

JavaScript
function findIntersection(arr1, arr2) {
    const intersection = [];
    arr1.forEach(item => {
        if (arr2.indexOf(item) !== -1 && !intersection.includes(item)) {
            intersection.push(item);
        }
    });
    return intersection;
}

// Example Usage
const arr1 = [3, 1, 5, 2];
const arr2 = [5, 2, 7];
console.log(findIntersection(arr1, arr2)); // Output: [5, 2]

Output

[5, 2]


Contact Us