Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection of two Arrays #36

Open
hrinal1994 opened this issue Sep 14, 2020 · 0 comments
Open

Intersection of two Arrays #36

hrinal1994 opened this issue Sep 14, 2020 · 0 comments

Comments

@hrinal1994
Copy link

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)]);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant