AlgoMaster Logo

Single Number II

Last Updated: March 29, 2026

medium

Understanding the Problem

You have an array where every number shows up exactly three times, except one lone number that appears just once. Your job is to find that single number.

If you have seen the classic Single Number problem (LeetCode 136), you know that when every duplicate appears twice, a simple XOR of all elements cancels out the pairs and leaves the unique one. But XOR cancels things in pairs, not triples. So the trick from Single Number does not directly work here.

The key question is: how do we "cancel out" elements that appear three times? The approaches below explore different ways to handle this, from straightforward counting all the way to an elegant bit manipulation trick that processes each bit position independently.

Key Constraints:

  • 1 <= nums.length <= 3 * 10^4 — With n up to 30,000, O(n^2) means roughly 900 million operations, which is borderline but likely too slow. O(n log n) is comfortable, and O(n) is ideal.
  • -2^31 <= nums[i] <= 2^31 - 1 — Full 32-bit integer range, including negatives. Any bit-level approach needs to handle negative numbers properly (32 bits, two's complement).
  • The problem explicitly asks for O(n) time and O(1) space, which rules out hash map counting as the final answer and pushes us toward bit manipulation.

Approach 1: Hash Map Counting

Intuition

The most straightforward idea: count how many times each number appears, then find the one with a count of 1. A hash map handles this cleanly in a single pass.

This is how you would solve it in production code. It does not meet the O(1) space requirement the problem asks for, but it is a solid starting point that is easy to reason about and hard to mess up.

Algorithm

  1. Create a hash map to store the count of each element.
  2. Iterate through the array, incrementing each element's count.
  3. Iterate through the hash map and find the element with count equal to 1.
  4. Return that element.

Example Walkthrough

1Initial array: nums = [2, 2, 3, 2]. Count each element.
0
2
1
2
2
3
3
2
1/6
1Empty count map
1/6

Code

The hash map approach meets the O(n) time requirement, but it uses O(n) extra space. The problem specifically asks for O(1) space. Can we figure out the answer without remembering every element?

Approach 2: Bitwise Counting (Bit-by-Bit)

Intuition

Here is the key insight: instead of thinking about whole numbers, think about each bit position independently.

Consider a single bit position, say bit 0 (the least significant bit). If you look at that bit across all elements in the array, each number that appears three times contributes either 0 or 3 to the total count of 1s at that position. Either way, the contribution from tripled elements is divisible by 3.

The single number's bit at that position adds either 0 or 1 to the total. So if we take the total count of 1s at bit position 0 and compute count % 3, the remainder tells us what the single number's bit is at that position.

We repeat this for all 32 bit positions, and we reconstruct the single number bit by bit.

Algorithm

  1. For each bit position from 0 to 31:

a. Count how many numbers in the array have a 1 at this bit position.

b. Take the count modulo 3.

c. If the remainder is 1, set that bit in the result.

  1. Return the result.

Example Walkthrough

1Binary: 2=010, 2=010, 3=011, 2=010. Process each bit position.
0
010
1
010
2
011
3
010
1/7
1result = 0 (binary: 000)
0
1/7

Code

The bit-by-bit approach already achieves O(n) time and O(1) space, but it makes 32 passes over the array. Can we process all 32 bits simultaneously in a single pass?

Approach 3: Bitmask State Machine (ones, twos)

Intuition

Instead of counting bits one position at a time, we want to process all 32 bits in parallel during a single pass. The idea is to build a "mod-3 counter" that works on entire integers at once.

Think of it this way: for each bit position, we need a counter that cycles through 0, 1, 2, and back to 0. That is a mod-3 counter. To represent three states (0, 1, 2), we need two bits. So we use two variables: ones and twos.

For each bit position independently:

  • ones = 0, twos = 0 means that bit has been seen 0 times (mod 3)
  • ones = 1, twos = 0 means that bit has been seen 1 time (mod 3)
  • ones = 0, twos = 1 means that bit has been seen 2 times (mod 3)
  • When the count reaches 3, both reset to 0

The update formulas are:

  • ones = (ones ^ num) & ~twos
  • twos = (twos ^ num) & ~ones

The XOR toggles bits as if we were doing a simple count. The AND with the complement acts as a mask that prevents a bit from being set in the wrong variable, implementing the three-state cycle. After processing all elements, ones holds the bits that appeared exactly once (mod 3), which is our answer.

Algorithm

  1. Initialize ones = 0 and twos = 0.
  2. For each number in the array:

a. Update ones: XOR with the number, then mask out any bits that are in twos.

b. Update twos: XOR with the number, then mask out any bits that are in ones.

  1. After processing all numbers, ones contains the single number.
  2. Return ones.

Example Walkthrough

1Initialize: ones=000, twos=000. Process each element.
0
2
1
2
2
3
3
2
1/6
1Initial state: ones=000, twos=000
ones
:
000
twos
:
000
1/6

Code