Keep Only First Appearances in an Integer List
Arrays & HashingAuto-graded · 21 test casesLast updated
Tests using a hash set alongside a list to filter repeated integers in one pass while preserving first-appearance order, in linear time.
Keep Only First Appearances
You are given a list of integers values. Build and return a new list that keeps each distinct integer exactly once, at the position of its first appearance in values. Every later copy of an integer is discarded. The surviving integers must keep the same relative order they had in the input.
The input list must not be modified; return a new list.
Rules
- If
valuesis empty, return an empty list. - If every integer is already distinct, the result has the same contents and order as the input.
- If all integers are equal, the result contains that integer exactly once.
- Negative numbers and zero are ordinary integers and are compared by value.
- Do not sort the data: sorting would destroy the order that must be preserved.
Constraints
0 <= len(values) <= 1,000,000-10^9 <= values[i] <= 10^9- Target time complexity:
O(n)expected, wherenis the length of the list. - Target extra space:
O(u), whereuis the number of distinct integers.
Examples
Example 1
Input: keep_first_occurrences([4, 5, 4, 1, 2, 5])
Output: [4, 5, 1, 2]
Example 2
Input: keep_first_occurrences([1, 1, 1, 1])
Output: [1]
Example 3
Input: keep_first_occurrences([])
Output: []
Hints
Hint 1
Walk through the list once from left to right and decide for each element whether it has appeared before.
Hint 2
Checking membership in a list is a linear scan, which can make the whole algorithm quadratic. Use a structure with constant-time expected lookups for the values already seen.
Hint 3
Keep two things: a set of values seen so far, and an output list. Append a value to the output (and add it to the set) only when it is not yet in the set.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Scan the list once from left to right while maintaining two structures:
seen, a hash set of integers already encountered, which gives expected constant-time membership checks.result, a list that collects integers in the order they first appear.
For each element, if it is not in seen, this is its first appearance: add it to seen and append it to result. Otherwise skip it.
Why it is correct
After processing the first i elements, result contains exactly the distinct values among them, ordered by first appearance, and seen contains the same values. An element already in seen has an earlier copy already placed in result, so skipping it is right. An element not in seen is a first appearance, and appending it places it after all values that first appeared earlier, so the order is correct. After the last element, result is the required answer. An empty input never enters the loop and yields an empty list. The input is never changed.
Avoid checking v not in result directly: that is a linear scan of a list and leads to O(n * u) time, which is too slow for a million elements.
Complexity
- Time:
O(n)expected, since each element costs one set lookup and at most one insert and one append. - Space:
O(u)for the set (and the output list), whereuis the number of distinct values.
Reference solution
from typing import List
def keep_first_occurrences(values: List[int]) -> List[int]:
seen = set()
result = []
for v in values:
if v not in seen:
seen.add(v)
result.append(v)
return result