This problem teaches the "seen tracking" pattern: maintaining a record of processed items.
You'll use this pattern for:
- Finding unique elements
- Detecting cycles in linked lists
- Checking if anagrams exist
- Counting distinct values
The set approach () beats the nested loop approach () because set lookups are .
Python 3.7+ preserves insertion order in dictionaries, so list(dict.fromkeys(items)) removes duplicates while maintaining order. Sets don't guarantee order.
Key insight: when order matters, use a list for results but a set for tracking what you've seen. You get both order preservation and fast lookups.