What is the easiest code for array intersection in JavaScript?


For array intersection, you can try to run the following code in JavaScript −

Example

<html>
   <body>
      <script>
         let intersection = function(x, y) {
            x = new Set(x), y = new Set(y);
            return [...x].filter(k => y.has(k));
         };

         document.write(intersection([5,7,4,8], [3,9,8,4,3]));
      </script>
   </body>
</html>

Updated on: 23-Jun-2020

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements