Skip to content

Intersection of two Arrays #36

Open
@hrinal1994

Description

@hrinal1994

Find below optimized code for intersection of two arrays:

var firstArray = [2, 2, 4, 1,5,2,3,6,4,22,2];
var secondArray = [1, 2, 0, 2];

intersection(firstArray, secondArray); // [2, 1]

function intersection(firstArray, secondArray) {

let finalArray=[];

for(let i=0;i<firstArray.length;i++){

if(secondArray.indexOf(firstArray[i])> -1){

  
  finalArray.push(firstArray[i]);
}

}

console.log([... new Set(finalArray)]);

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions