Remove Duplicates While Preserving Order

Medium ⏱ 12 min 62% acceptance ★★★★☆ 4.3
Write dedupe_preserve_order(items) that returns a new list with duplicate values removed, keeping only the first occurrence of each value and preserving the original order. Do not use set() directly on the output (since sets don't preserve order) — track what has been seen instead.

Examples

Example 1
Input
items = [3, 1, 3, 2, 1, 4]
Output
[3, 1, 2, 4]
Explanation

Each value is kept only the first time it appears.

Constraints

  • 0 <= len(items) <= 10^5
  • Elements are hashable.

Topics

ListsDeduplication

Companies

MicrosoftUber

Hints

Hint 1

Keep a set of values already seen while building the result list.

Hint 2

Skip an item if it is already in the seen set; otherwise add it to both the result and the seen set.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.