Open In App

Find Common Elements In Three Sorted Arrays using JavaScript

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript can be used to find the common elements in given three sorted arrays. We are given three different sorted arrays, we have to return common elements of three arrays.

Example:

Input 
array1 = [1 , 2 , 3 , 4 ,5 ]
array2 = [3 , 4, 5 , 6 ,7 ]
array3 = [ 3 , 4 , 7 , 8 , 9]

Output
[3 , 4]

Below are different approaches to finding common elements present in three sorted arrays:

Brute force Approach

We will initialize three pointers to traverse each element of the array simultaneously. Now we iterate through the arrays using the pointers within its size limits and compare element present at the current positions of pointers and push common elements into the common array. Now we increase pointers comparing elements present in three arrays and then return the common array.

Example: To demonstrate finding common elements present in three sorted arrays using three pointers approach

JavaScript
function CommonElements(arr1, arr2, arr3) {
    const common = [];
    let i = 0, j = 0, k = 0;

    while (i < arr1.length
        && j < arr2.length
        && k < arr3.length
    ) {
        if (
            arr1[i] === arr2[j]
            &&
            arr2[j] === arr3[k]
        ) {
            common.push(arr1[i]);
            i++;
            j++;
            k++;
        }
        else if (arr1[i] < arr2[j]) {
            i++;
        }
        else if (arr2[j] < arr3[k]) {
            j++;
        }
        else {
            k++;
        }
    }

    return common;
}

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const arr3 = [3, 4, 5, 8, 1];

console
    .log("common elements in three array are :",
    CommonElements(arr1, arr2, arr3)); 

Output
common elements in three array are : [ 4, 5 ]

Time complexity: O(n) , n is total numbers of elements across all three arrays

Space complexity : O(m) , m is common elements present in arrays

Using set

We will create sets from all three input arrays to remove duplicate elements. Now we Iterate through the elements of the first set and check for each element that it exist in both second and third sets. if element exist in both sets then we add it to common array. Return the common array.

Example : To demonstrate finding common elements present in three sorted arrays using set

JavaScript
function CommonElements(arr1, arr2, arr3) {
    const set1 = new Set(arr1);
    const set2 = new Set(arr2);
    const set3 = new Set(arr3);
    const common = [];

    for (const elem of set1) {
        if (
            set2.has(elem)
            &&
            set3.has(elem)) {
            common.push(elem);
        }
    }

    return common;
}

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const arr3 = [2, 4, 5, 1];

console
    .log("common elements in three array using set : ",
    CommonElements(arr1, arr2, arr3));

Output
common elements in three array using set :  [ 4, 5 ]

Time complexity : O(n+m)

Space complexity: O(n+m)

Using Hashmap

We will create a hash map to store the count of occurrences of elements from arr1. Now we Iterate through arr2, if an element is found in map, push it into the common array and decrement its count in map. Finally we iterate through arr3 and similarly push elements into finalCommon array if found in map and decrement their count. Return the finalCommon array

Example : To demonstrate finding common elements present in three sorted arrays using hashmap.

JavaScript
function findCommonElements(arr1, arr2, arr3) {
    const map1 = {};
    const map2 = {};
    const map3 = {};
    const common = [];

    for (const num of arr1) {
        map1[num] = (map1[num]
            ||
            0
        ) + 1;
    }

    for (const num of arr2) {
        map2[num] = (
            map2[num]
            ||
            0
        ) + 1;
    }

    for (const num of arr3) {
        map3[num] = (
            map3[num]
            ||
            0
        ) + 1;
    }

    for (const num of arr1) {
        if (
            map1[num]
            &&
            map2[num]
            &&
            map3[num]
        ) {
            common
                .push(num);
            map1[num]--;
            map2[num]--;
            map3[num]--;
        }
    }

    return common;
}

const arr1 = [1, 2, 3, 4, 5];
const arr2 = [4, 5, 6, 7, 8];
const arr3 = [1, 4, 5, 2, 6];

console
    .log("common elements in three array is : ",
    findCommonElements(arr1, arr2, arr3)); 

Output
common elements in three array is :  [ 4, 5 ]

Time complexity : O(n + m + p)

Space complexity : O(min(n,m,p))



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads