Single Number II — Solution in Python, Java & C++
Single Number II solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Single Number II is a medium coding problem on arrays and bit manipulation, asked in Amazon, Google, Microsoft and Bloomberg interviews.
- Difficulty: Medium
- Topics: Arrays, Bit Manipulation
- Asked at: Amazon, Google, Microsoft, Bloomberg
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an integer array nums where every element appears exactly three times except for one, which appears once, find that single element.
Solve it in linear time using constant extra space.
Example 1
Input: nums = [2,2,3,2]
Output: 3
Example 2
Input: nums = [0,1,0,1,0,1,99]
Output: 99
Example 3
Input: nums = [-2,-2,1,1,-3,1,-3,-3,-4,-2]
Output: -4
Constraints
1 <= nums.length <= 40-1000000 <= nums[i] <= 1000000Every element appears three times except one, which appears once.
Hints for Single Number II
Hint 1
XOR alone does not work — it cancels pairs, not triples.
Hint 2
Look at each of the 32 bit positions independently and count how many numbers have that bit set.
Hint 3
That count is a multiple of 3 plus the loner's bit, so
count % 3recovers the answer bit by bit; sign-extend for negatives.
How to solve Single Number II
Count, for each of the 32 bit positions, how many numbers have that bit set. Every triple contributes a multiple of three, so count % 3 is the loner's bit.
Approach
- For each bit
0..31, sum(x >> bit) & 1over the array. - If the sum is not divisible by 3, set that bit in the answer.
- Sign-extend: if bit 31 is set, subtract
2³².
Why it works
Bits are independent under counting: the tripled numbers add 0 or 3 to each position's tally, so the residue mod 3 is exactly the single number's bit.
Complexity
- Time —
O(32 · n) - Space —
O(1)
Pitfalls
- Plain XOR cancels pairs, not triples.
- Forgetting the sign extension returns a large positive value for a negative loner.
Reference solution
Python
def singleNumber(nums) -> int:
answer = 0
for bit in range(32):
ones = sum((x >> bit) & 1 for x in nums)
if ones % 3:
answer |= 1 << bit
if answer >= (1 << 31):
answer -= 1 << 32
return answerJava
public static int singleNumber(int[] nums) {
int answer = 0;
for (int bit = 0; bit < 32; bit++) {
int ones = 0;
for (int x : nums) ones += (x >> bit) & 1;
if (ones % 3 != 0) answer |= 1 << bit;
}
return answer;
}C++
int singleNumber(vector<int>& nums) {
int answer = 0;
for (int bit = 0; bit < 32; bit++) {
int ones = 0;
for (int x : nums) ones += (x >> bit) & 1;
if (ones % 3 != 0) answer |= 1 << bit;
}
return answer;
}JavaScript
var singleNumber = function(nums) {
let answer = 0;
for (let bit = 0; bit < 32; bit++) {
let ones = 0;
for (let i = 0; i < nums.length; i++) ones += (nums[i] >> bit) & 1;
if (ones % 3 !== 0) answer |= 1 << bit;
}
return answer | 0;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Bit Manipulation