Skip to content

Instantly share code, notes, and snippets.

@eduarmreyes
Created August 8, 2019 15:23
Show Gist options
  • Save eduarmreyes/b277353c2b87407566c8f612b42c3563 to your computer and use it in GitHub Desktop.
Save eduarmreyes/b277353c2b87407566c8f612b42c3563 to your computer and use it in GitHub Desktop.
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