Single Number

Easy#136xor

You get an array of integers in which every number occurs exactly twice, except one that occurs once. Find that one, in one pass and with O(1) extra memory. Our example: [10, 3, 7, 3, 10] — the answer is 7.

input
nums: an array of integers in which every number comes twice, except one that comes once.
returns
The number that comes once.
constraints
  • 1 ≤ nums.length ≤ 3001
  • −3·10⁴ ≤ nums[i] ≤ 3·10⁴
  • every number comes exactly twice, except one that comes once
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} nums
 * @return {number}
 */
function singleNumber(nums) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more bit manipulation problems

Pattern “Bit manipulation” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.