You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
Return the single element that appears only once.
Your solution must run in O(log n)
time and O(1)
space.
Example 1:
Input: nums = [1,1,2,3,3,4,4,8,8] Output: 2
Example 2:
Input: nums = [3,3,7,7,10,11,11] Output: 10
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 105
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 goal is to find the single number that appears only once in a sorted list where every other number appears exactly twice. The brute force strategy involves checking each number individually to see if it's the unique one.
Here's how the algorithm would work step-by-step:
def single_non_duplicate(numbers):
list_length = len(numbers)
index = 0
while index < list_length - 1:
# Check if current element is different from the next.
if numbers[index] != numbers[index + 1]:
return numbers[index]
# Skip the next element since it's a duplicate.
index += 2
# If the unique element is at the end of the list.
return numbers[list_length - 1]
The key to solving this efficiently lies in recognizing the sorted nature of the list. Instead of checking each number individually, we can use a divide-and-conquer approach to quickly narrow down the search to the unique element.
Here's how the algorithm would work step-by-step:
def find_single_element(numbers):
left_index = 0
right_index = len(numbers) - 1
while left_index < right_index:
middle_index = (left_index + right_index) // 2
# Ensure middle_index is even to compare pairs correctly.
if middle_index % 2 != 0:
middle_index -= 1
# Unique element is in the right half.
if numbers[middle_index] != numbers[middle_index + 1]:
right_index = middle_index
# Unique element is in the left half.
else:
left_index = middle_index + 2
return numbers[left_index]
Case | How to Handle |
---|---|
Null or empty array | Return an appropriate error value (e.g., -1) or throw an exception indicating invalid input. |
Array with only one element | Return the single element directly as it must be the unique element. |
Array with only two elements (both different) | Return the first element since only one unique element can exist, and the array cannot be valid according to the problem description. |
Single element at the beginning of the array | The binary search condition needs to handle the boundary condition where the single element is at index 0. |
Single element at the end of the array | The binary search condition needs to handle the boundary condition where the single element is at the last index. |
Array with all elements being the same except for one at either end | Binary search needs to correctly narrow the search space when encountering long sequences of the same number. |
Large input array to test for performance (time complexity) | Binary search ensures logarithmic time complexity, even for large arrays. |
Integer overflow when calculating mid in binary search | Calculate mid as low + (high - low) / 2 to prevent potential integer overflow. |