Order-Preserving Unique Values from a Mixed List
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests using a hash-based structure to drop repeated values from a list while keeping first-occurrence order, in linear time and without modifying the input.
Order-Preserving Unique Values
You receive a list whose items are simple values: integers or strings. Build and return a new list that holds each distinct value exactly once.
The values in the result must be arranged in the order in which they first showed up in the input. Any later repeat of a value is discarded.
The input list must remain unchanged.
Details
- Two values count as the same only if they are equal under Python's normal
==and hashing rules. For example, the integer1and the string"1"are different values. - If the input is empty, return an empty list.
- The result must be a different list object from the input, even when all values are already distinct.
- Aim for O(n) average time, where n is the length of the input, using O(n) extra space.
Constraints
- The list may contain integers, strings, or a mix of both.
- The list length is at most 1,000,000.
- Every item is a primitive value (no nested lists, dicts, or custom objects).
Examples
Example 1
Input: unique_in_order([1, 2, 2, 3, 4, 1, 5])
Output: [1, 2, 3, 4, 5]
Example 2
Input: unique_in_order(['apple', 'orange', 'apple', 'banana', 'orange'])
Output: ['apple', 'orange', 'banana']
Hints
Hint 1
Checking whether a value already exists in the output list is O(n) per check and leads to O(n^2) overall. Use a structure with O(1) average membership tests.
Hint 2
A set tells you whether a value was seen, but a set alone does not keep order. Combine a set with an output list, appended to only on the first sighting.
Hint 3
Python dicts remember insertion order, and inserting an existing key does not move it. Think about how to build a dict from the input and read its keys back.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through the list from left to right while remembering which values have already been seen. The first time a value appears, add it to the result. Whenever it appears again, ignore it.
Python's dict is a convenient tool here. It offers O(1) average lookups and, since Python 3.7, it guarantees that keys stay in insertion order. Calling dict.fromkeys(items) inserts every item as a key in input order. Inserting a key that is already present does not change where that key sits, so the first occurrence alone determines the ordering. Converting the keys to a list gives the answer.
An equivalent explicit version uses a set for membership checks and a list for output:
seen = set()
result = []
for x in items:
if x not in seen:
seen.add(x)
result.append(x)
return result
Why it is correct
- Uniqueness: a value is emitted only when it is not yet recorded, and it is recorded right after, so it cannot be emitted twice.
- Completeness: every distinct value has a first occurrence, and at that moment it is not yet recorded, so it is emitted.
- Order: the scan goes left to right, and emission happens at first occurrences, so the output follows first-occurrence order.
- No mutation: the input is only read, and a fresh list is built.
Complexity
- Time: O(n) on average, one hash lookup or insert per element.
- Space: O(u), where u is the number of distinct values (at most n), for the dict and the output list.
A naive approach that tests x in result on a list costs O(n^2), which is far too slow for a million elements. Using list(set(items)) is also wrong because sets do not preserve order.
Reference solution
from typing import List
def unique_in_order(items: List) -> List:
# dict keeps insertion order; re-inserting an existing key leaves its
# position unchanged, so each value stays at its first-occurrence slot.
return list(dict.fromkeys(items))