Array Intersection

EasyArrays10 min4 tests

Implement an intersection function that returns the common elements between two arrays.

Requirements

  • Return elements present in both arrays
  • No duplicates in the output
  • Order should follow the first array

Example

intersection([1, 2, 3, 4], [3, 4, 5, 6]); // [3, 4]
Hints (3)
  1. Convert one array to a Set for O(1) lookups.
  2. Filter the other array for elements that exist in the Set.
  3. Use another Set or filter logic to avoid duplicates.

Topics

  • Arrays

Asked at

Amazon · Microsoft · Adobe

Join Us
blur