Practice · Bit manipulation · 3 of 3

Find the Unpaired Number

In the list nums, every value appears exactly twice, except one value that appears only once. Return that value.

Aim for a single pass that uses only a constant amount of extra memory.

Example 1

Input
nums = [5, 3, 5]
Output
3

Example 2

Input
nums = [8, -2, 7, 8, 7]
Output
-2
8 and 7 each appear twice; -2 appears once.

Constraints

  • nums holds 1 to 29999 numbers.
  • −10⁹ ≤ nums[i] ≤ 10⁹
  • Every value appears twice, except exactly one that appears once.