Bitwise XOR
LC #260Medium
Single Number III
Bitwise XOR
AmazonGoogleProblem
Two elements appear once, all others appear twice. Find those two elements.
arraybit-manipulation
Constraints
- ›2 ≤ n ≤ 3 × 10⁴
- ›-2³¹ ≤ nums[i] ≤ 2³¹ − 1
- ›Exactly two elements appear once; all others appear twice
- ›O(1) extra space required
Example
Input
nums = [1, 2, 1, 3, 2, 5]Output
[3, 5]Why
3 and 5 appear once. All others appear twice. XOR all → 3^5, then split