Problem 267200 · easy · Phase 02 Linear Data Structures

Remove Duplicates, Keep First Seen

arrays · sets · ordering

Given a list nums, return a new list containing each distinct value once, in the order in which it first appears.

Examples

Input:  nums = [3, 1, 3, 2, 1]
Output: [3, 1, 2]

Input:  nums = [2, 2, 2, 2]
Output: [2]

Constraints

  • 0 <= len(nums) <= 10**5
  • Elements are hashable (ints or strings).
  • Return a new list; do not modify nums.
  • Target: O(n) time. Checking membership in a list is too slow for the largest inputs.

Goals

  • Track seen values in a set for O(1) membership tests
  • Preserve the original order of first appearances
  • Avoid the O(n^2) `x not in list` pattern
Starting Python…