Find the k non-repeating elements in a list with little additional space
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding the first k non-repeating elements sounds simple until the space constraint is added. The main tradeoff is that exact answers require remembering frequency information somewhere, so the real goal is not zero extra space, but the smallest reasonable auxiliary storage for the assumptions you have about the input.
Exact Answers Need Counting Information
If the list can contain arbitrary values and the original order must be preserved, you need a way to know whether each value appears once or more than once. For unrestricted inputs, that means storing counts in a hash table or dictionary keyed by value.
Output:
This uses O(n) time and O(u) additional space, where u is the number of distinct values. For general inputs, that is usually the correct answer.
Why “Little Additional Space” Has Limits
People often ask whether the problem can be solved with constant extra space. In the general case, not if you need exact results and stable ordering. Without storing frequency information or modifying the input in a substantial way, the algorithm has no reliable memory of which values have already been seen and how often.
So the better engineering answer is to choose the smallest structure that matches the domain:
- dictionary for arbitrary hashable values
- fixed-size array for a small known numeric range
- in-place sorting only if order does not need to be preserved
When the Value Range Is Small
If the values are integers in a known bounded range, an array can be more space-efficient than a dictionary.
This still stores counts, but it replaces hash-table overhead with indexed storage. It only makes sense when the numeric range is small enough to justify the array.
In-Place Approaches Change the Tradeoff
If preserving original order is not required, sorting can reduce certain costs, especially in languages where in-place sort is available and you are allowed to mutate the input. After sorting, repeated values become adjacent, making it easy to identify singles.
That can reduce explicit auxiliary storage, but you lose the original sequence order. For many interview-style problems, that distinction determines whether the solution is acceptable.
Streaming Is Harder Than It Looks
If the list arrives as a stream and you want the first k non-repeating elements at the end, you still need delayed certainty. An element that has appeared once so far may repeat later. That means one-pass streaming without state is impossible for exact results. You need retained counts or a data structure that tracks both counts and arrival order.
The simplest exact solution is still the two-pass counting approach.
Common Pitfalls
- Chasing constant extra space for arbitrary inputs when the problem fundamentally requires frequency information.
- Forgetting that preserving input order changes which algorithms are valid.
- Using sorting to reduce space without noticing that it changes the original sequence.
- Stopping at the first
kdistinct elements instead of the firstkelements that appear exactly once. - Choosing a fixed-size count array when the input range is too large or unknown.
Summary
- For general inputs, the standard exact solution is a two-pass count and collect approach.
- The space cost is
O(u), which is usually the minimum practical cost for preserving order. - A fixed-size count array is useful only when the numeric range is known and small.
- Sorting can reduce some overhead, but it changes the input order and therefore changes the problem.
- The right answer depends on whether order preservation, mutability, and value range are constrained.

