Single Number

Tries & Bit Manipulation, problem 1 of 8

Single Number

Easy

LC #136

bit manipulationxor

Not attempted yet

In nums, every value appears exactly twice except for one value that appears once. Return that lonely value.

Aim for O(n) time and O(1) extra space.

Example 1

Input: nums = [2, 2, 1]
Output: 1

Example 2

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

Example 3

Input: nums = [1]
Output: 1

Constraints

  • 1 ≤ len(nums) ≤ 2 · 10⁵ (odd length)
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • exactly one value appears once

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.