Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement