2Brute2Best
🔥0

Single Number II

Medium·LeetCode #137 Pro OnlyMedium
LeetCode
01

Problem Statement

You get an array nums. Every value in it appears three times, except for one value that appears exactly once. Return that single value.

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

Input

An integer array nums with at least one value. Every value appears three times except one, which appears once.

Output

The one value that appears exactly once.

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

2 shows up three times. The 3 is the one that shows up once.

Example 2
Input: nums = [0, 1, 0, 1, 0, 1, 99]
Output: 99

0 and 1 each show up three times. The 99 is the single one.

Constraints
  • • 1 ≤ nums.length ≤ 3 · 10^4
  • • −2^31 ≤ nums[i] ≤ 2^31 − 1
  • • Every value appears exactly three times, except one value that appears once.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“every element appears three times except one”
→ Triples do not cancel under XOR. Count the bits instead.
“linear time, constant extra space”
→ 32 counters (or two variables), not a hash map.
“return the single one”
→ The answer is rebuilt from its bits, one column at a time.
03

Approach Complexity Ladder

Brute Force — count each value with a loopO(N²)O(1)

For each value, scan the array and return the one that appears exactly once

Better — count with a hash mapO(N)O(N)

Count each value once in a hash map, then return the value with count 1

Optimal — count the bits, then take count % 3O(32 · N)O(1)

For each of the 32 bit positions, count the 1s; set the answer's bit when the count % 3 is 1

Brute Force Intuition

For each value in nums, scan the whole array and count how many times it appears. The first value whose count is 1 is the answer.

Recognition rule for this sub-pattern:

x ^ x = 0 and x ^ 0 = x, so XOR-ing everything cancels the pairs.
Remember this · Bit Manipulation
Trigger
"Every element appears twice except…", power of two, counting set bits, subsets as bitmasks, O(1) space tricks.
Think
What happens to this problem if I look at each bit separately?
Key question
Which bit operation (XOR, AND, OR, shift) cancels or isolates what I need?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.