Skip to content
MediumArraysAI interview only

Single Number III

Asked atgoogleamazonmetamicrosoftadobe

01 · Problem

You are given an integer array nums in which exactly two values appear exactly once and every other value appears exactly twice. Return the two values that appear once, as an array of two integers. The two values may be returned in any order.

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

02 · Examples

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

1 and 2 each appear twice, leaving 3 and 5. [5,3] is also accepted.

Example 02
Input
nums = [-1,0]
Output
[-1,0]

Both values appear once, so both are returned.

Example 03
Input
nums = [4,1,2,1,2,7]
Output
[4,7]

1 and 2 are paired up, leaving 4 and 7.

03 · Constraints

  • 012 <= nums.length <= 3 * 104
  • 02-231 <= nums[i] <= 231 - 1
  • 03Exactly two values appear 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.