Deduplicate a List While Preserving Order
Problem statement
You are given an integer array values.
Return a new array containing each distinct value exactly once. Preserve the order in which values first appear.
Function
deduplicateList(values: int[]) → int[]Examples
Example 1
values = [4,2,4,1,2,3]return = [4,2,1,3]Keep the first occurrence of each value and discard later duplicates.
Example 2
values = [-1,-1,0,-1,0]return = [-1,0]The first -1 and the first 0 determine the output order.
Example 3
values = [7]return = [7]A one-element array is already distinct.
Constraints
1 <= values.length <= 10^5.-10^9 <= values[i] <= 10^9.