JavaMediumCollections20 min
Find the distinct intersection of two arrays
Print common distinct values in ascending order, space-separated; print an empty line if the intersection is empty. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe boundary handling and collection selection.
Input
n, n integers, m, then m integers.
Output
Print common distinct values in ascending order, space-separated; print an empty line if the intersection is empty.
Constraints
0 <= n,m <= 100000; values in [-1000000000,1000000000].
Example
Input
4 3 1 2 2 4 2 3 3 8
Output
2 3
Example 2 input
0 2 1 2
Output
—
This example exercises a further boundary or ordering case.
Public test cases (2)
Input
4 3 1 2 2 4 2 3 3 8
Expected output
2 3
Input
2 1 2 2 3 4
Expected output
—
Hints (2)
- Identify the state needed to avoid recomputing work for each element.
- Use a hash set for membership and a TreeSet to deduplicate and order the shared values.
Target complexity: time O(n+m log(k+1)) expected · space O(n+k)
