Given an integer array nums
, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
Example 1:
Input: nums = [1,2,1,3,2,5] Output: [3,5] Explanation: [5, 3] is also a valid answer.
Example 2:
Input: nums = [-1,0] Output: [-1,0]
Example 3:
Input: nums = [0,1] Output: [1,0]
Constraints:
2 <= nums.length <= 3 * 104
-231 <= nums[i] <= 231 - 1
nums
will appear twice, only two integers will appear once.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem involves checking every number against every other number to see if it has a duplicate. By exhaustively comparing each number to all the others, we can identify the two numbers that appear only once.
Here's how the algorithm would work step-by-step:
def single_number_iii_brute_force(numbers):
single_numbers = []
for first_index in range(len(numbers)):
is_single = True
# Check if the current number is a duplicate
for second_index in range(len(numbers)):
if first_index != second_index and numbers[first_index] == numbers[second_index]:
is_single = False
break
# Add to result if no duplicates found
if is_single:
single_numbers.append(numbers[first_index])
return single_numbers
The goal is to find the two unique numbers in a list where all other numbers appear twice. The trick is to use bitwise operations to isolate the difference between the two unique numbers and then separate the numbers into two groups based on that difference.
Here's how the algorithm would work step-by-step:
def single_number_iii(numbers):
xor_sum = 0
for number in numbers:
xor_sum ^= number
# Find the rightmost set bit
rightmost_bit = xor_sum & -xor_sum
number1 = 0
number2 = 0
# Divide numbers into two groups
for number in numbers:
if number & rightmost_bit:
number1 ^= number
else:
number2 ^= number
return [number1, number2]
Case | How to Handle |
---|---|
Empty input array | Return an empty array because there are no single numbers to find. |
Array with fewer than 2 elements | Return an empty array since there can't be two single numbers. |
Array with exactly two distinct elements | The bit manipulation solution should correctly identify and return those two elements. |
Array contains negative numbers, zeros, or very large positive/negative numbers | The bitwise XOR operations should work correctly with these number ranges within the typical integer limits. |
Integer overflow during XOR operation when two extremely large numbers are XORed | The code should ideally use a data type capable of handling large numbers to avoid overflow, or implement overflow checks. |
All numbers are the same except for the two single numbers | The XOR and bit masking operations will still isolate the two single numbers correctly. |
Array contains only one unique number occurring twice | The result will be incorrect, the algorithm expects two unique numbers that appears once. |
Large input array affecting time complexity | The bit manipulation solution scales linearly, O(n), so it should perform adequately even with large inputs. |