260. Single Number III
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 asks us to find two unique integers in an array where every other integer appears exactly twice. The key constraints are a linear time complexity (O(n)) and constant extra space complexity (O(1)). This rules out solutions involving hash maps for counting frequencies (which would use O(n) space) or sorting (which is typically O(n log n) time).
Solution
Single Number III in Python
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
# Step 1: XOR all numbers to get the XOR sum of the two unique numbers, a ^ b.
xor_sum = 0
for num in nums:
xor_sum ^= num
# Step 2: Find the rightmost set bit in xor_sum.
# This bit is different between a and b.
# In Python, integers have arbitrary precision, so -xor_sum works as expected
# without overflow issues. The expression `x & -x` isolates the least significant bit.
diff_bit = xor_sum & -xor_sum
# Step 3: Partition the numbers into two groups and find the single numbers.
num1, num2 = 0, 0
for num in nums:
# If the differentiating bit is set in num, XOR it into the first group.
if num & diff_bit:
num1 ^= num
# If the differentiating bit is not set, XOR it into the second group.
else:
num2 ^= num
return [num1, num2]
Single Number III in Java
class Solution {
public int[] singleNumber(int[] nums) {
// Step 1: XOR all numbers to get a ^ b.
int xor_sum = 0;
for (int num : nums) {
xor_sum ^= num;
}
// Step 2: Find the rightmost set bit in a ^ b.
// This bit is different between the two single numbers.
// The expression `x & -x` isolates the least significant bit.
int diff_bit = xor_sum & -xor_sum;
// Step 3: Partition the numbers into two groups and find the single numbers.
int[] result = {0, 0};
for (int num : nums) {
// Group 1: numbers where the diff_bit is not set.
if ((num & diff_bit) == 0) {
result[0] ^= num;
}
// Group 2: numbers where the diff_bit is set.
else {
result[1] ^= num;
}
}
return result;
}
}
Single Number III in C++
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
// Step 1: XOR all numbers to get a ^ b.
// Using long long for xor_sum to avoid potential overflow issues with INT_MIN,
// although for this specific problem `int` would likely suffice.
long long xor_sum = 0;
for (int num : nums) {
xor_sum ^= num;
}
// Step 2: Find the rightmost set bit in a ^ b.
// This bit is different between a and b. It's also known as the least significant bit (LSB).
// The expression `x & -x` isolates the LSB.
long long diff_bit = xor_sum & -xor_sum;
// Step 3: Partition the numbers into two groups and find the single numbers.
vector<int> result = {0, 0};
for (int num : nums) {
// If the differentiating bit is not set in the current number,
// it belongs to the first group.
if ((num & diff_bit) == 0) {
result[0] ^= num;
} else { // If the differentiating bit is set, it belongs to the second group.
result[1] ^= num;
}
}
return result;
}
};
Single Number III in TypeScript
function singleNumber(nums: number[]): number[] {
// Step 1: XOR all numbers to get the XOR of the two unique numbers, a ^ b.
let xor_sum = 0;
for (const num of nums) {
xor_sum ^= num;
}
// Step 2: Find the rightmost set bit in xor_sum.
// This bit is a '1' in xor_sum, meaning it's a position where
// the two unique numbers have different bits.
// The expression `x & -x` isolates the least significant bit.
const diff_bit = xor_sum & -xor_sum;
// Step 3: Partition the numbers into two groups and find the unique numbers.
let num1 = 0;
let num2 = 0;
for (const num of nums) {
// If the differentiating bit is set in the current number,
// it belongs to the first group.
if ((num & diff_bit) !== 0) {
num1 ^= num;
} else {
// Otherwise, it belongs to the second group.
num2 ^= num;
}
}
// The result is the two unique numbers.
return [num1, num2];
};
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.