DEV Community

Stylus07
Stylus07

Posted on

1 1

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay