Given a non-empty array of integers nums
, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
1 <= nums.length <= 3 * 104
-3 * 104 <= nums[i] <= 3 * 104
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 finding the single number involves checking each number in the list against all the other numbers. We try to find a match for each number, and the one that doesn't have a match is the single number.
Here's how the algorithm would work step-by-step:
def find_single_number_brute_force(numbers):
for current_number_index in range(len(numbers)):
current_number = numbers[current_number_index]
found_match = False
# Compare current number to the others
for other_number_index in range(len(numbers)):
# Don't compare the number to itself
if current_number_index != other_number_index:
other_number = numbers[other_number_index]
if current_number == other_number:
found_match = True
# Move to next number if we found a match
break
# Return the number if no match was found
if not found_match:
# The current number is the single number
return current_number
The problem asks to find the unique number in a list where every other number appears exactly twice. The clever approach leverages a mathematical trick to efficiently identify the single number without needing to count occurrences or compare every pair. It uses the properties of a special operation.
Here's how the algorithm would work step-by-step:
def find_single_number(numbers):
single_number = 0
# Use XOR to find the single number.
for number in numbers:
# XOR operation cancels out pairs.
single_number ^= number
#The remaining value is the single number
return single_number
Case | How to Handle |
---|---|
Null or empty input array | Return 0 or throw an exception, depending on problem specifications, as there is no single number in an empty array. |
Array with only one element | Return the single element directly, as it is trivially the single number. |
Array with maximum allowed size (considering memory constraints) | Ensure the chosen algorithm (e.g., XOR) has acceptable time and space complexity for large inputs; avoid memory-intensive solutions like large hash maps. |
All elements are the same (invalid input according to the problem description) | The XOR solution would return the repeating element; the hashmap solution would not populate, so a 0 (if returned) or error might be needed to indicate a violation of constraints. |
Array contains negative numbers, zero, and positive numbers | XOR solution handles all these cases directly; Hash map solution handles all these cases without modification. |
Integer overflow with XOR operation (if numbers are very large) | Consider using a language with arbitrary-precision integers or check for overflow conditions if the XOR operation might exceed integer limits. |
Array contains the maximum possible integer value | The XOR solution handles this correctly; hashmap needs to accommodate this without memory overflow. |
Highly skewed distribution with a large range of numbers | XOR solution still works efficiently; hash map needs to scale efficiently in terms of hash function and memory usage. |