Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. function intersection(array1, array2) {
  2. const intersectionOfArrays = [];
  3. const cache = {};
  4. array1.forEach(element => {
  5. cache[element] = element; /// filling up my dictionary
  6. });
  7. array2.forEach(element => {
  8. if (cache[element]) {
  9. intersectionOfArrays.push(element);
  10. }
  11. });
  12. return intersectionOfArrays;
  13. }
  14.  
  15. const array1 = [1, 3, 5, 2];
  16. const array2 = [4, 6, 5, 3];
  17.  
  18. const result = intersection(array1, array2);
  19.  
  20. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement