Single Number

Implement singleNumber

Somewhere in this array, exactly one value breaks the pattern — everything else shows up in a matching pair, but one number stands alone with no partner. Find it. Order doesn't matter here, and there's no need to track which values you've already seen: XOR-ing every element together in a single pass cancels every paired value down to nothing (since x ^ x = 0), leaving only the unpaired value behind.

Example 1:

Input: nums = [2,2,1]

Output: 1

Example 2:

Input: nums = [4,1,2,1,2]

Output: 4

Example 3:

Input: nums = [1]

Output: 1

+ 10 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 3 × 10⁴
  • -3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴
  • Every value in nums appears exactly twice, except for one value which appears exactly once

nums =

[2, 2, 1]