Unique Array
JS Functions
Easy
10 min
arrays
sets
lodash
Implement uniqueArray(array). It returns a new array with duplicates removed. Each value appears once, at the position of its first occurrence.
uniqueArray([1, 2, 1, 3, 2]); // [1, 2, 3]
uniqueArray(['b', 'a', 'b']); // ['b', 'a']
uniqueArray([NaN, NaN, 0, -0]); // [NaN, 0]
uniqueArray([1, '1']); // [1, '1'] (different types are different values)
const a = { id: 1 };
uniqueArray([a, a, { id: 1 }]); // [a, { id: 1 }] (objects by reference)
Requirements
- Compare with SameValueZero:
NaNequalsNaN,0equals-0, and no type coercion (1and'1'are different). Objects and arrays are equal only if they're the same reference. - Keep the first occurrence and the original relative order.
- Return a new array. Don't mutate the input.
- Aim for O(n) time.
Follow-up
- Solve it without
Set,Mapor a lookup object. What's the complexity now? (This variant has been reported in real interviews.) - If the array is already sorted, how can you do it in O(n) time and O(1) extra space?
- How would you de-duplicate a list of objects by
id, keeping the last occurrence?
Spotted a mistake or a rights concern? Report an issue