Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Bit Manipulation Medium frequency Solution Available Practice Mode

Single Number II LeetCode Solution

Tags: #Array #Bit Manipulation

Approach Summary

Sum bits at each position modulo 3. The bit that does not cancel is the unique number's bit. Use two variables (ones, twos) for a clean state machine.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Bit Manipulation Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Bit Manipulation pattern.

Framework: Bit Manipulation • Interview Prep: Tested at FAANG • Explore all Bit Manipulation problems & invariants →

How to Recognize This Pattern

  • Every element appears 3× except one
  • Bit-count mod 3 at each position — 32 separate counters or state machine

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(1)

Tags

Array Bit Manipulation

Frequently Asked Questions

What is the optimal time and space complexity for Single Number II?
The optimal solution for Single Number II achieves O(n) runtime complexity and requires O(1) auxiliary space. Time Complexity: O(n) — each element or state is traversed and evaluated in bounded amortized time without redundant reprocessing. Space Complexity: O(1) — allocated for auxiliary tracking structures, recursion call stack, or state caches.
How do I identify that Single Number II requires the Bit Manipulation pattern?
In technical interviews, look for key problem signals: Every element appears 3× except one; Bit-count mod 3 at each position — 32 separate counters or state machine. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Bit Manipulation strategy.
What critical edge cases must be handled in Single Number II?
Essential edge cases to verify include: Key recognition trigger: Every element appears 3× except one.; Constraint condition: Bit-count mod 3 at each position — 32 separate counters or state machine.; Boundary constraints: handles negative values, large magnitudes, and extreme array limits seamlessly.; Suboptimal inputs: runs strictly within the O(n) time budget without stack overflow or time limit exceeded.. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Single Number II?
Single Number II is a Medium-difficulty question tagged with medium recurrence. It is actively asked in technical screening and onsite interview loops at Amazon, Facebook.
Buy me a coffee
Daily Practice
0/0