Bitwise XOR
LC #136Easy
Single Number
Bitwise XOR
AmazonGoogleMicrosoftAppleProblem
Every element appears twice except one. Find that single element using O(1) space.
arraybit-manipulation
Constraints
- ›1 ≤ n ≤ 3 × 10⁴
- ›-3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴
- ›Exactly one element appears once; all others appear twice
- ›O(1) extra space required
Example
Input
nums = [4, 1, 2, 1, 2]Output
4Why
1^1=0, 2^2=0, 0^4=4. Every pair cancels; only 4 remains