Filter Duplicates While Preserving Order
Problem statement
Given an integer array data, return a new array containing each distinct value exactly once.
Preserve the order of first appearance: when a value occurs multiple times, keep only its earliest occurrence.
Function
filterDuplicates(data: int[]) → int[]Examples
Example 1
data = [7,6,4,3,3,4,9]return = [7,6,4,3,9]The second 3 and second 4 are removed. Every retained value stays in its first-appearance position.
Example 2
data = [5,5,5]return = [5]Only the first occurrence of 5 is retained.
Example 3
data = [-2,0,-2,4,0]return = [-2,0,4]Negative values and zero follow the same first-occurrence rule.
Constraints
datais notnull.- Every element of
datais an integer.