Coding · Easy · Free
Single number — every element appears twice except one (XOR)
CodingBit ManipulationFresher
Approach
- XOR is commutative; pairs cancel to 0.
- Accumulate XOR of all elements.
Java Solution
Java
int singleNumber(int[] nums) {
int x = 0;
for (int n : nums) x ^= n;
return x;
}| Complexity | |
|---|---|
| Time | O(n) |
| Space | O(1) |
Interview tip: Mention overflow is not an issue for XOR on ints in Java.