JavaScriptEasyArrays10 min
Keep unique array values in order
Remove duplicate integers, preserving the order of their first appearance. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess use Set insertion order to preserve first occurrences.
Input
A JSON array of integers on stdin.
Output
Print one compact JSON value: an array of integers.
Constraints
0 <= array length <= 10000; integers between -1000000 and 1000000.
Example
Input
[3,1,3,2,1]
Output
[3,1,2]
Example 2 input
[1000000,-1000000,1000000]
Output
[1000000,-1000000]
Boundary case: An empty input produces an empty result
Public test cases (2)
Input
[3,1,3,2,1]
Expected output
[3,1,2]
Input
[1,1]
Expected output
[1]
Hints (2)
- A Set stores each numeric value once
- An empty input produces an empty result
Target complexity: time O(n) · space O(n)
