JavaScriptMediumHashing20 min
Intersect two arrays without duplicates
Return distinct values present in both arrays, ordered by their first occurrence in the first array. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess delete matched Set entries to suppress duplicate output.
Input
A JSON array [firstArray, secondArray].
Output
Print one compact JSON value: an array of distinct integers.
Constraints
Each array has at most 10000 integers between -1000000 and 1000000.
Example
Input
[[3,1,3,2],[2,3,3]]
Output
[3,2]
Example 2 input
[[7],[]]
Output
[]
Boundary case: Scan the first array to preserve its order
Public test cases (2)
Input
[[3,1,3,2],[2,3,3]]
Expected output
[3,2]
Input
[[1],[2]]
Expected output
[]
Hints (2)
- Build membership from the second array
- Scan the first array to preserve its order
Target complexity: time O(n+m) · space O(m)
