137. Single Number II
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem requires us to find a single element in an array where every other element appears exactly three times. The solution must have linear time complexity (O(n)) and use constant extra space (O(1)). Standard counting methods, like using a hash map, would violate the space constraint. This strongly suggests a bit manipulation approach is necessary.
Solution
Single Number II in Python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
# 'ones' stores the bits that have appeared 1, 4, 7, ... times
# 'twos' stores the bits that have appeared 2, 5, 8, ... times
ones = 0
twos = 0
for num in nums:
# The new 'ones' is a bitwise XOR of the old 'ones' and the current number,
# but only for the bits that are not set in 'twos'.
# If a bit in 'num' is set and it's not in 'twos', it might go into 'ones'.
# If a bit is already in 'ones', XORing with 'num' will clear it.
ones = (ones ^ num) & ~twos
# The new 'twos' is a bitwise XOR of the old 'twos' and the current number,
# but only for the bits that are not set in the *new* 'ones'.
# This logic correctly implements a mod-3 counter for each bit position.
# A bit appears once: set in 'ones'.
# Appears twice: cleared from 'ones', set in 'twos'.
# Appears three times: cleared from 'twos'.
twos = (twos ^ num) & ~ones
# The bits of the single number will have appeared once, so they will be in 'ones'.
# The bits of the numbers appearing three times will be cleared from both 'ones' and 'twos'.
return ones
Single Number II in Java
class Solution {
public int singleNumber(int[] nums) {
// 'ones' stores the bits that have appeared 1, 4, 7, ... times
// 'twos' stores the bits that have appeared 2, 5, 8, ... times
int ones = 0;
int twos = 0;
for (int num : nums) {
// The new 'ones' is a bitwise XOR of the old 'ones' and the current number,
// but only for the bits that are not set in 'twos'.
// If a bit in 'num' is set and it's not in 'twos', it might go into 'ones'.
// If a bit is already in 'ones', XORing with 'num' will clear it.
ones = (ones ^ num) & ~twos;
// The new 'twos' is a bitwise XOR of the old 'twos' and the current number,
// but only for the bits that are not set in the *new* 'ones'.
// This logic correctly implements a mod-3 counter for each bit position.
// A bit appears once: set in 'ones'.
// Appears twice: cleared from 'ones', set in 'twos'.
// Appears three times: cleared from 'twos'.
twos = (twos ^ num) & ~ones;
}
// The bits of the single number will have appeared once, so they will be in 'ones'.
// The bits of the numbers appearing three times will be cleared from both 'ones' and 'twos'.
return ones;
}
}
Single Number II in C++
class Solution {
public:
int singleNumber(vector<int>& nums) {
// 'ones' stores the bits that have appeared 1, 4, 7, ... times
// 'twos' stores the bits that have appeared 2, 5, 8, ... times
int ones = 0, twos = 0;
for (int num : nums) {
// The new 'ones' is a bitwise XOR of the old 'ones' and the current number,
// but only for the bits that are not set in 'twos'.
// If a bit in 'num' is set and it's not in 'twos', it might go into 'ones'.
// If a bit is already in 'ones', XORing with 'num' will clear it.
ones = (ones ^ num) & ~twos;
// The new 'twos' is a bitwise XOR of the old 'twos' and the current number,
// but only for the bits that are not set in the *new* 'ones'.
// This logic correctly implements a mod-3 counter for each bit position.
// A bit appears once: set in 'ones'.
// Appears twice: cleared from 'ones', set in 'twos'.
// Appears three times: cleared from 'twos'.
twos = (twos ^ num) & ~ones;
}
// The bits of the single number will have appeared once, so they will be in 'ones'.
// The bits of the numbers appearing three times will be cleared from both 'ones' and 'twos'.
return ones;
}
};
Single Number II in TypeScript
function singleNumber(nums: number[]): number {
// 'ones' stores the bits that have appeared 1, 4, 7, ... times
// 'twos' stores the bits that have appeared 2, 5, 8, ... times
let ones = 0;
let twos = 0;
for (const num of nums) {
// The new 'ones' is a bitwise XOR of the old 'ones' and the current number,
// but only for the bits that are not set in 'twos'.
// If a bit in 'num' is set and it's not in 'twos', it might go into 'ones'.
// If a bit is already in 'ones', XORing with 'num' will clear it.
ones = (ones ^ num) & ~twos;
// The new 'twos' is a bitwise XOR of the old 'twos' and the current number,
// but only for the bits that are not set in the *new* 'ones'.
// This logic correctly implements a mod-3 counter for each bit position.
// A bit appears once: set in 'ones'.
// Appears twice: cleared from 'ones', set in 'twos'.
// Appears three times: cleared from 'twos'.
twos = (twos ^ num) & ~ones;
}
// The bits of the single number will have appeared once, so they will be in 'ones'.
// The bits of the numbers appearing three times will be cleared from both 'ones' and 'twos'.
return ones;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.