PythonEasyCollections10 min
Remove duplicate values while preserving order
Do not sort the input. An empty array produces an empty output line. Interviewers use this problem to assess combine a membership set with an ordered output list.
Input
First line: n. Second line: n space-separated integers (empty when n = 0).
Output
Print the result values separated by single spaces; print an empty line for an empty result.
Constraints
0 <= n <= 100000; -10^9 <= each value <= 10^9.
Example
Input
7 3 1 3 2 1 4 2
Output
3 1 2 4
Example 2 input
4 1000000000 -1000000000 0 1000000000
Output
1000000000 -1000000000 0
Large signed values retain their original order.
Public test cases (2)
Input
7 3 1 3 2 1 4 2
Expected output
3 1 2 4
Input
4 -1 0 -1 0
Expected output
-1 0
Hints (2)
- Identify the state needed to avoid repeated work.
- Combine a membership set with an ordered output list.
Target complexity: time O(n) expected · space O(n)
