Single Number

You are given a non-empty array of integers nums. Every value in it appears exactly twice, except for one value that appears exactly once. Return that value.

Your solution should run in linear time and use only constant extra space.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4
  • Every value appears exactly twice, except one value that appears exactly once.

Examples

Example 1

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

7 and 3 each appear twice and cancel out under XOR; 5 appears once.

Example 2

Input: nums = [9]
Output: 9

A single element is the answer by itself.

Example 3

Input: nums = [2,-4,2]
Output: -4

Negative numbers work the same way: 2 ^ -4 ^ 2 = -4.

JavaScript you'll use: Bitwise operators

/**
 * @param {number[]} nums
 * @return {number}
 */
function singleNumber(nums) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.