Remove Duplicates While Preserving First-Appearance Order
UpstartSoftware Engineer · Online AssessmentLast updated
Return each array value once in its original first-appearance order, using membership tracking without sorting the result.
Remove duplicate values from an array while preserving the order in which each distinct value first appears.
Function Contract
Implement filter_duplicates(data) -> list[int].
Return a new array containing the first occurrence of each distinct value in the input's original order.
Constraints and Clarifications
For a portable practice interface, values are integers.
datais always an array, never null.0 <= len(data) <= 200000.- Values range from
-1000000000through1000000000. - Equal values count as duplicates even when separated by other values.
- Do not sort the result.
- Aim for expected
O(n)time.
Examples
data = [7, 6, 4, 3, 3, 4, 9]
Output: [7, 6, 4, 3, 9]
data = [0, -1, 0, -1, 2]
Output: [0, -1, 2]
Hints
Hint 1
Track prior appearances without changing traversal order: Decide whether to emit a value when you first encounter it. The membership structure and the ordered result have different responsibilities.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.