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)
- Convert one array to a Set for O(1) lookups.
- Filter the other array for elements that exist in the Set.
- Use another Set or filter logic to avoid duplicates.
Topics
- Arrays
Asked at
Amazon · Microsoft · Adobe
