JavaMediumCollections20 min
Remove array duplicates while preserving order
Print distinct values in first-appearance order, space-separated; print an empty line when n=0. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe boundary handling and collection selection.
Input
n followed by n integers.
Output
Print distinct values in first-appearance order, space-separated; print an empty line when n=0.
Constraints
0 <= n <= 100000; values in [-1000000000,1000000000].
Example
Input
6 3 1 3 2 1 4
Output
3 1 2 4
Example 2 input
4 1 2 3 4
Output
1 2 3 4
This example exercises a further boundary or ordering case.
Public test cases (2)
Input
6 3 1 3 2 1 4
Expected output
3 1 2 4
Input
4 5 5 5 5
Expected output
5
Hints (2)
- Identify the state needed to avoid recomputing work for each element.
- Insert into a LinkedHashSet to combine deduplication with insertion order.
Target complexity: time O(n) expected · space O(n)
