Deduplicate an Infinite Stream

Medium ⏱ 12 min 48% acceptance ★★★★★ 4.5
Write the generator unique(stream) yielding each element only the first time it appears, preserving order, working lazily on unbounded streams (a seen-set grows, but nothing is precomputed). Elements are hashable.

Examples

Example 1
Input
list(unique(['a', 'b', 'a', 'c', 'b']))
Output
['a', 'b', 'c']
Explanation

Repeats are filtered as they flow past.

Constraints

  • Lazy — must work on infinite streams.
  • First occurrence wins.

Topics

Iterators & Generatorsstateful filter

Companies

Kafka VendorsConfluentLinkedIn

Hints

Hint 1

A set of seen items plus a conditional yield.

Hint 2

Add after yielding or before — just be consistent.

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