Find the Smallest Integer Not in a List
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The phrase "smallest integer not in a list" sounds simple, but it needs one extra assumption. If the list may contain any integers, then the problem has no answer, because there is always a smaller missing negative number.
In practice, this question almost always means "find the smallest positive integer not present in the list." Once that is clear, there are two standard solutions: a simple set-based approach and an in-place linear-time approach.
Clarify the Problem First
Suppose the list is [1, 2, 4, 5]. The smallest positive integer missing from the list is 3.
Suppose the list is [0, -1, 2]. The smallest positive integer missing from the list is 1, because values less than 1 do not matter for this version of the problem.
That positive-only assumption is what makes the problem well-defined and useful in interviews and production code alike.
Simple and Readable Set-Based Solution
The easiest correct solution is to place the values in a set, then count upward from 1 until you find a number that is not present.
This runs in O(n) average time because set lookup is constant time on average, and it uses O(n) extra memory for the set.
For most real applications, this is the best solution. It is short, easy to review, and hard to get wrong.
In-Place O(n) Time and O(1) Extra Space
If you need the classic interview-optimized solution, you can use the array itself to track which values from 1 through n are present. The general idea is:
- Ignore numbers that are non-positive or greater than
n. - Use index positions to mark which positive values exist.
- Scan for the first index that was never marked.
Here is a working Python implementation:
Why does this work? In a list of length n, the smallest missing positive integer must be in the range from 1 through n + 1. If every value from 1 through n is present, then the answer is n + 1. Otherwise, the first missing value inside that range is the answer.
When Sorting Is Acceptable
Another reasonable approach is to sort the numbers, then scan for the first gap. That is easy to understand but costs O(n log n) time:
This can be perfectly fine when input sizes are small or clarity matters more than asymptotic optimality.
Common Pitfalls
- Forgetting to define the domain. Without the "positive integer" restriction, the problem is not well-posed.
- Starting from
0instead of1when the expected answer is the smallest missing positive integer. - Failing on duplicates such as
[1, 1, 2, 2]. A correct algorithm must handle repeated values cleanly. - Mishandling negative numbers or zeros. They should be ignored for the positive-only version.
- Choosing the in-place algorithm when readability matters more than minimizing memory usage.
Summary
- The meaningful version of this problem is usually "smallest missing positive integer."
- A set-based solution is the simplest correct
O(n)approach for most codebases. - The in-place marking algorithm also runs in
O(n)time and uses constant extra space. - Sorting is easier to reason about but slower on large inputs.
- Before writing code, make sure the problem statement is precise enough to have a real answer.
Related reading
- Find the smallest number that is greater than a given number in a sorted list
- Find the smallest positive integer that does not occur in a given sequence
- Find the smallest regular number that is not less than N
- Find the smallest set of overlapping jobs
- Find the subarray with the max XOR from an array using a trie
- Find the top k sums of two sorted arrays
- Find the smallest unique substring for each string in an array
- Find the sum of all numbers between 1 and N divisible by either x or y

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.