DEV Community

Stylus07
Stylus07

Posted on

Intersection of Three Sorted Arrays

Problem:

Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays.

var arraysIntersection = function(arr1, arr2, arr3) {
    let p1=0;
    let p2=0;
    let p3=0;

    let ans = [];

    while(p1 < arr1.length && p2< arr2.length  && p3 < arr3.length){

        if(arr1[p1] === arr2[p2] && arr2[p2] === arr3[p3]){
            ans.push(arr1[p1]);
            p1++;
            p2++;
            p3++;
        }else{
            if(arr1[p1] < arr2[p2]){
                p1++;
            }else if(arr2[p2] < arr3[p3]){
                p2++;
            }else{
                p3++;
            }
        }
    }
    return ans;
};
Enter fullscreen mode Exit fullscreen mode

Discussion (0)