Given n-1n array, find missing number
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
This is a classic interview problem: you have an array with n - 1 numbers taken from the range 1 through n, and exactly one number is missing. The goal is to recover the missing value efficiently.
The simplest solutions use arithmetic or XOR. Both give linear time, and the best choice usually comes down to readability versus bit-manipulation comfort.
Sum Formula Method
The sum of numbers from 1 through n is:
If you subtract the actual array sum from the expected sum, the difference is the missing number.
Python example:
This prints 6.
This is easy to explain and usually the most readable solution.
XOR Method
The XOR approach is another O(n) time and O(1) extra-space solution.
The key identity is:
If you XOR all numbers in the array and all numbers from 1 through n, every number that appears in both places cancels out. The missing number is the only value left.
This also prints 6.
The XOR method is a strong interview answer because it avoids overflow concerns in languages where the sum formula could exceed integer limits.
Hash Set Method
If you want a direct correctness-first solution and extra space is acceptable, a set works too:
This is also easy to understand, but it uses O(n) extra space instead of O(1).
Which Method Should You Prefer
Use the sum formula when:
- readability matters most
- integer overflow is not a concern
- and the input constraints are simple
Use XOR when:
- you want
O(1)extra space - you want to avoid overflow in fixed-width integer languages
- or the problem is explicitly interview-style
Use a set when:
- you want a very direct implementation
- the input rules may later become more flexible
- and extra memory is acceptable
What If the Range Starts at Zero
Some versions of the problem use numbers from 0 through n instead of 1 through n. The same strategies still work, but the expected range changes.
For example:
This prints 3.
Always check the exact problem statement before applying the formula automatically.
Common Pitfalls
One common mistake is using the wrong range. A formula for 1 through n is not the same as a formula for 0 through n.
Another mistake is assuming the array always satisfies the problem contract. If the input can contain duplicates or out-of-range values, the classic missing-number tricks may produce misleading answers.
It is also easy to prefer a clever XOR solution when a straightforward sum solution would be clearer for the team maintaining the code.
Finally, in fixed-width integer languages, the sum formula can overflow for large n unless you choose a sufficiently large numeric type. Python avoids that issue because integers grow automatically.
Summary
- The missing number can be found in linear time with either arithmetic or XOR.
- The sum formula is often the clearest solution.
- XOR gives the same time complexity with constant extra space and avoids overflow concerns in some languages.
- A set-based solution is simple but uses more memory.
- Always confirm whether the range is
1throughnor0throughnbefore implementing the formula.
Related reading
- Given n and k, return the kth permutation sequence
- Given numbers from 1 to 232-1, one is missing. How to find the missing number optimally?
- Given parallel lists, how can I sort one while permuting rearranging the other in the same way?
- Given Prime Number N, Compute the Next Prime?
- Given that HashMaps in jdk1.6 and above cause problems with multithreading, how should I fix my code
- Given two arrays A and Q, foreach element of of Q, find the element in A with smallest difference
- Given some AABBs, find minimum total surface area AABBs that contain them all?
- Given two arrays, find the permutations that give closest distance between two arrays

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.