#136
Easy Algorithms Single number
Array Bit Manipulation
77.4% acceptance
Jan 12, 2026
18687
888
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn single_number(nums: Vec<i32>) -> i32 {
// XOR all numbers: duplicates cancel out, leaving the single number
nums.iter().fold(0, |acc, &num| acc ^ num)
}
}