Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@w8r
Created July 31, 2021 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w8r/2dac2d8f04d9e817be072f3caa11d169 to your computer and use it in GitHub Desktop.
Save w8r/2dac2d8f04d9e817be072f3caa11d169 to your computer and use it in GitHub Desktop.
Array intersection
function intersection(...lists) {
const result = [];
const resultLUT = {};
for(let i = 0; i < lists.length; i++) {
const currentList = lists[i];
for(let y = 0; y < currentList.length; y++) {
const currentValue = currentList[y];
if(!resultLUT[currentValue]) {
let existsInAll = true;
for(let x = 0; x < lists.length; x++) {
if(lists[x].indexOf(currentValue) === -1) {
existsInAll = false;
break;
}
}
if(existsInAll) {
result.push(currentValue);
}
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment