noodleProblems/
Single Number
#164

Single Number

AlgorithmeasyBit ManipulationArray

You're given a non-empty integer array nums where every element appears exactly **twice**, except for one element that appears exactly **once**. Find and return that single element.

Your solution should run in linear time and use only constant extra space (no hash map or counting structure).

Example cases

  • single value in the middle
    in nums = [2,2,1]
    out 1
  • single value at the start
    in nums = [4,1,2,1,2]
    out 4
    1 and 2 each appear twice; 4 appears once.
  • one-element array
    in nums = [1]
    out 1

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4
  • Every element appears exactly twice, except for one element which appears exactly once.
Saved
nums =
[2,2,1]