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 * 104When 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:
Imagine you have a pile of items where almost everything comes in matching pairs, but one single item is left without a partner. The brute force strategy involves picking up each item one by one and exhaustively searching through the entire pile to see if a matching partner exists.
Here's how the algorithm would work step-by-step:
def single_number(number_pile):
# Iterate through each item to check if its pair exists elsewhere in the pile
for current_index in range(len(number_pile)):
match_found = False
for search_index in range(len(number_pile)):
if current_index == search_index:
continue
if number_pile[current_index] == number_pile[search_index]:
match_found = True
break
# Return immediately if we find an item without a matching partner
if not match_found:
return number_pile[current_index]Instead of counting how many times each item appears, the optimal approach uses a clever trick of magic pairing. By combining every item together using this special method, all the items that appear twice cancel each other out, leaving only the single lonely item behind.
Here's how the algorithm would work step-by-step:
def single_number(list_of_numbers):
# Initialize the magical glowing box with a neutral starting value of zero.
glowing_box_value = 0
# Process every single item sequentially to allow identical pairs to interact.
for current_number in list_of_numbers:
glowing_box_value ^= current_number
# Only the lonely item remains because all paired numbers canceled out.
return glowing_box_value| Case | How to Handle |
|---|---|
| Minimum valid input with a single element | The XOR accumulator starts at zero and processes the single element, correctly returning that exact number. |
| Array containing negative numbers | Bitwise XOR operates identically on the binary representation of negative integers, correctly canceling out pairs. |
| Array containing zero | Zero XORed with any number equals that number, allowing zeros to pair up or stand alone without disrupting the accumulator. |
| Very large arrays nearing memory or time limits | A single pass linear scan with O(1) space complexity executes efficiently within optimal time and memory constraints. |
| Inputs with mixed positive and negative numbers containing extreme boundary values | Bitwise operations naturally handle integer limits without causing overflow or underflow issues. |
| The single unique number is located at the very beginning or end of the array | The commutative and associative properties of XOR ensure the unique element is isolated regardless of its position. |