SHARE
TWEET

Untitled

a guest Aug 20th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top