Quickest way to find missing number in an array of numbers
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Finding the missing number in an array is a common problem that can be encountered in technical interviews and algorithm exercises. The quickest and most efficient solution often depends on the specific nature of the array and constraints. This article delves into various methods to solve this problem, focusing on the most rapid approach, supported with technical explanations and practical examples.
Introduction
In a typical scenario, you may have an array with a sequence of numbers, say from 1 to `n`, with one number missing. The challenge is to identify that missing number efficiently. Several methods get the job done, ranging from simple iteration to more mathematically intuitive approaches.
Method 1: Using the Sum Formula
The most efficient way, generally speaking, is to use the formula for the sum of an arithmetic series. The sum of numbers from 1 to `n` is given by the formula:
Given an array that goes from 1 to `n`, but missing one number, the process is simple:
- Calculate the expected sum using the formula.
- Find the actual sum of the array.
- The difference between the expected sum and the actual sum gives the missing number.
Example
Consider the array `[1, 2, 4, 5, 6]`.
- Calculate expected sum for `n=6`:
- Calculate actual sum:
- The missing number:
This method is both quick and doesn't require extra space, making it an time complexity solution with space complexity.
Method 2: Using XOR Operation
A secondary approach involves XOR (exclusive OR) operations. Leveraging XOR properties can achieve this with similar time complexity and no additional space.
The XOR operation has a few useful properties:
- `a ⊕ a = 0` for any integer `a`.
- `a ⊕ 0 = a` for any integer `a`.
- XOR is commutative and associative.
The approach:
- XOR all numbers from 1 to `n`.
- XOR all numbers in the array.
- XOR the results from steps 1 and 2. The missing number is obtained by the properties described above.
Example
For the array `[1, 2, 4, 5, 6]` and `n=6`:
- XOR from 1 to 6:`1 ⊕ 2 ⊕ 3 ⊕ 4 ⊕ 5 ⊕ 6 = 7`
- XOR the array:`1 ⊕ 2 ⊕ 4 ⊕ 5 ⊕ 6 = 2`
- XOR of above results:`7 ⊕ 2 = 3`
This method again features time complexity and space complexity.
Comparison of Methods
| Method | Time Complexity | Space Complexity | Pros | Cons |
| Sum Formula | Simple math operation, clear logic | May face integer overflow for large n | ||
| XOR Operation | Avoids overflow issues, clever use of XOR | May be less intuitive to understand for beginners |
Conclusion
Both methods offered are efficient, with their own advantages and constraints. The sum formula method is straightforward but can face integer overflow issues with large numbers. The XOR method is a clever bit-manipulation hack that bypasses math constraints but may be less immediately intuitive. Choosing the right one depends on the problem constraints and personal comfort with the underlying techniques.
In practice, either method will typically perform exceedingly well in relevant problem scenarios. Familiarity with both can prove advantageous for tackling an array of related algorithmic challenges.
Related reading
- Quickly checking if set is superset of stored sets
- Quickselect Algorithm - Simplified Explanation
- QuickSelect Algorithm Understanding
- QuickSelect with Hoare partition scheme
- quicksort algorithm stability
- Quicksort superiority over Heap Sort
- Quicksort - Hoare's partitioning with duplicate values
- Quicksort - which sub-part should be sorted first?

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.