Neural Mastery
← Practice
Deduplicate List Preserving Ordereasy

Deduplicate List Preserving Order

easy Python for AI 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Deduplicate a sequence while strictly maintaining order -- standard `set(items)` destroys ordering, but ranking algorithms and prompt context builders need uniqueness without scrambling priority.

Related Skills

Task

Implement deduplicate_preserve_order(items): return a new list containing only the unique elements of items, in the exact order of their first appearance. If items is empty, return [].

Function Signature

deduplicate_preserve_order(items: list) -> list

Examples

Example 1: Interleaved Duplicate Strings
Input: items = ['b', 'a', 'c', 'a', 'b', 'd', 'c']
Output: ['b', 'a', 'c', 'd']
Example 2: Duplicate Integers
Input: items = [4, 5, 4, 1, 2, 5, 3]
Output: [4, 5, 1, 2, 3]

Constraints

  • Libraries are allowed; a plain-Python solution earns 10 bonus points.
  • Must run in O(n) expected time.
  • First-appearance order must be preserved.
  • Empty input list returns [].
Python3Saved ✓
def deduplicate_preserve_order(items: list) -> list:
"""Remove duplicates from a list while maintaining original first-appearance order.

Unlike set(items), strictly preserves the order of elements as they
first appeared in items. Returns an empty list for empty input."""
# Your implementation here
pass

Case 1: Interleaved Duplicate Strings
Input: items = ['b', 'a', 'c', 'a', 'b', 'd', 'c']
Expected: ['b', 'a', 'c', 'd']
Case 2: Duplicate Integers
Input: items = [4, 5, 4, 1, 2, 5, 3]
Expected: [4, 5, 1, 2, 3]