Created
August 8, 2019 15:23
-
-
Save eduarmreyes/b277353c2b87407566c8f612b42c3563 to your computer and use it in GitHub Desktop.
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(array1, array2) { | |
const intersectionOfArrays = []; | |
const cache = {}; | |
array1.forEach(element => { | |
cache[element] = element; /// filling up my dictionary | |
}); | |
array2.forEach(element => { | |
if (cache[element]) { | |
intersectionOfArrays.push(element); | |
} | |
}); | |
return intersectionOfArrays; | |
} | |
const array1 = [1, 3, 5, 2]; | |
const array2 = [4, 6, 5, 3]; | |
const result = intersection(array1, array2); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment