#268
Easy Algorithms Missing number
Array Hash Table Math Binary Search Bit Manipulation Sorting
71.7% acceptance
Jan 12, 2026
14190
3467
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn missing_number(nums: Vec<i32>) -> i32 {
let n = nums.len() as i32;
let expected_sum = n * (n + 1) / 2;
let actual_sum: i32 = nums.iter().sum();
expected_sum - actual_sum
}
}