Set vs List: Deduping Without Losing Order

Medium ⏱ 12 min 55% acceptance ★★★★☆ 4.4
Write a function dedupe_preserve_order(items) that removes duplicate values from list items while preserving the order of first appearance. A plain set(items) would remove duplicates but scramble the order — your function must not do that.

Examples

Example 1
Input
dedupe_preserve_order([3, 1, 3, 2, 1])
Output
[3, 1, 2]
Explanation

Each value kept only at its first occurrence, order preserved.

Example 2
Input
dedupe_preserve_order([])
Output
[]
Explanation

Empty input stays empty.

Constraints

  • items contains hashable values.

Topics

Variables & Data Typessets vs lists

Companies

LinkedInAdobe

Hints

Hint 1

Track values you have already seen in a set for O(1) lookup.

Hint 2

Build the result list by appending only unseen values.

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