Skip to content
MediumArraysAI interview only

Single Number II

Asked atgoogleamazonmicrosoftadobeapple

01 · Problem

You are given a non-empty integer array nums in which every value appears exactly three times, except for one value that appears exactly once. Return that single value.

Your solution should run in linear time and use only constant extra space. Values may be negative.

02 · Examples

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

2 appears three times and 3 appears once.

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

0 and 1 each appear three times; 99 appears once.

Example 03
Input
nums = [-4,7,-4,-4]
Output
7

-4 appears three times, leaving 7 as the single value.

03 · Constraints

  • 011 <= nums.length <= 3 * 104
  • 02nums.length % 3 == 1
  • 03-231 <= nums[i] <= 231 - 1
  • 04Exactly one value appears once; every other value appears exactly three times

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.