Skip to content
EasyArraysAI interview only

Single Number

Asked atamazongoogleapplemicrosoftadobebloomberg

01 · Problem

You are given a non-empty integer array nums in which every value occurs exactly twice, except for one value that occurs exactly once. Return that lone value.

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

02 · Examples

Example 01
Input
nums = [2,2,1]
Output
1

2 appears twice and 1 appears once.

Example 02
Input
nums = [4,1,2,1,2]
Output
4

1 and 2 each appear twice, leaving 4 as the only unpaired value.

Example 03
Input
nums = [1]
Output
1

With a single element, that element is the answer.

03 · Constraints

  • 011 <= nums.length <= 3 * 104
  • 02nums.length is odd
  • 03-3 * 104 <= nums[i] <= 3 * 104
  • 04Exactly one value appears once; every other value appears exactly twice

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.