Created
July 31, 2021 19:47
-
-
Save w8r/2dac2d8f04d9e817be072f3caa11d169 to your computer and use it in GitHub Desktop.
Array intersection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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