S1Q3 · Find Missing Number in a Range¶
⚡ Quick Reference
Function: find_missing_number(nums: list, n: int) -> int
Core idea: the sum of 1 to n is n*(n+1)//2. Subtract the actual sum of the list to get the missing number.
Key rules:
- Expected sum of 1..n = n*(n+1)//2
- Missing number = expected sum − actual sum
- O(n) time, O(1) space - no sorting or set needed
Problem Statement¶
Problem
Write a function find_missing_number(nums, n) that finds the single missing number from a list that should contain all integers from 1 to n.
Example:
nums=[1, 4, 2, 4, 6, 5, 6], n=6
3
Understanding the approach¶
The sum of all integers from 1 to n is given by the formula:
For n = 6: expected = 6 × 7 / 2 = 21
Actual sum of [1, 4, 2, 4, 6, 5, 6] = 28... wait, that's larger than expected.
The list has duplicates (4 and 6 appear twice) - one duplicate takes the place of the missing number 3. So:
But actually the formula still works directly:
Hmm - let me re-read. The list has n numbers (one missing, one duplicate). So:
actual - expected = 28 - 21 = 7. The duplicate value minus the missing value = 7. But we need: missing = duplicate - 7... this doesn't resolve without knowing the duplicate.
This example has a duplicate - use set approach
The example [1, 4, 2, 4, 6, 5, 6] has duplicates (4 and 6). The sum formula only works when there is exactly one missing and no duplicates. The set approach works for both cases:
set(range(1, n+1)) is the full expected set; subtracting set(nums) leaves only the missing number.
Tracing the example¶
nums = [1, 4, 2, 4, 6, 5, 6], n = 6
Expected set: {1, 2, 3, 4, 5, 6}
Actual set: {1, 2, 4, 5, 6} (sets deduplicate automatically)
Difference: {3}
Missing: 3 ✓
Solution approaches¶
Works correctly even when nums contains duplicates. The set difference gives exactly the missing element.
def find_missing_number(nums: list, n: int) -> int:
expected = n * (n + 1) // 2
return expected - sum(nums)
Works when the list has exactly one missing element and no duplicates. O(1) extra space.
Key takeaways¶
Set difference handles duplicates safely
set(range(1, n+1)) - set(nums) works regardless of duplicates in nums because sets automatically deduplicate. The result is always the single missing number.
Sum formula - fast but assumes no duplicates
n*(n+1)//2 - sum(nums) is O(n) time and O(1) space. It only works correctly when the list has exactly one missing number and no duplicate values.
set.pop() retrieves the single element
After the set difference, only one element remains. .pop() removes and returns it. Since there's guaranteed to be exactly one missing number, this is safe.