Sequence NODE_153
Medium

Find Intersection of Two Arrays

JavaScript
Technical Specification

Return an array containing the unique intersection of two arrays (values present in both).

Input/Output Samples
Input:[1,2,2,3], [2,3,4]
Output:[2,3]
Optimal Logic Path
function arrayIntersection(a, b) {
  const setB = new Set(b);
  const result = new Set();
  for (const val of a) {
    if (setB.has(val)) result.add(val);
  }
  return [...result];
}
Architectural Deep-Dive
We exploit constant-time Set lookup and then ensure we only add each intersecting value once.