Single Number
- 1 min read
- LC-Easy
- Bit-manipulation
Solution
- The XOR of any number with itself is 0.
- This, XOR every number, and the number that remains is the number that occurs once.
| Time | Space | Explanation |
|---|
O(n) | O(1) | |
def singleNumber(self, nums: List[int]) -> int:
acc = 0
for num in nums:
acc ^= num
return acc