Single Number
EasyBit Manipulation · Paypal
You are given a non-empty integer array nums. Every value appears exactly twice except for one value, which appears exactly once. Return that value.
Your solution should run in O(n) time and use only O(1) extra space (so no hash set or counter).
Example 1
- Input:
- nums = [5,3,9,3,5]
- Output:
- 9
- Explanation
- 5 and 3 each appear twice, so they pair off. 9 appears once and is the answer.
Example 2
- Input:
- nums = [-1,2,2]
- Output:
- -1
- Explanation
- 2 is paired, which leaves -1. Negative values are handled the same way.
Example 3
- Input:
- nums = [7]
- Output:
- 7
- Explanation
- With only one element, it has no partner and is the answer.
Constraints
1 <= nums.length <= 3 * 10^4-3 * 10^4 <= nums[i] <= 3 * 10^4- Every value appears exactly twice except one, which appears once