Single Number

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write singleNumber(nums): in the array nums, every value appears exactly twice except one, which appears once. Return that one. The values are whole numbers from -2,147,483,648 to 2,147,483,647, and nums holds at least one number. Don't change nums. For example, singleNumber([4, 1, 2, 1, 2]) is 4. The checks include about 100,000 numbers, so counting each value by scanning the whole array again is too slow.

Your code must pass

  • Finds the number that appears once
  • Handles negative numbers and zero
  • Handles a single number
  • Is fast enough for about 100,000 numbers