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 <= 1050 <= nums[i] <= 105When 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 long row of items where almost every item appears in an exact pair next to each other, except for one single item that appears alone. The brute force approach involves inspecting the items one by one from the very beginning to find the odd one out.
Here's how the algorithm would work step-by-step:
def single_non_duplicate(nums):
current_index = 0
# We stop before the last element to prevent looking out of bounds
while current_index < len(nums) - 1:
if nums[current_index] != nums[current_index + 1]:
# If neighbors do not match, this is the single element
return nums[current_index]
current_index += 2
# If no mismatch was found earlier, the final element is unique
return nums[-1]Instead of checking every single item one by one, we can use a smart elimination strategy to find the unique item quickly. By looking at the middle of our ordered collection, we can rule out half of the items in a single step.
Here's how the algorithm would work step-by-step:
def single_non_duplicate(sorted_array):
left_pointer = 0
right_pointer = len(sorted_array) - 1
while left_pointer < right_pointer:
middle_index = left_pointer + (right_pointer - left_pointer) // 2
# Ensure middle_index is even so we can check pairs correctly aligned in the sorted array.
if middle_index % 2 == 1:
middle_index -= 1
# If the pair matches, the single element must be located further to the right side.
if sorted_array[middle_index] == sorted_array[middle_index + 1]:
left_pointer = middle_index + 2
# If the pair does not match, the single element is located somewhere on the left side.
else:
right_pointer = middle_index
return sorted_array[left_pointer]| Case | How to Handle |
|---|---|
| Array with a single element | Binary search boundaries immediately evaluate the lone element without entering the loop, returning it correctly. |
| Single element located at the very beginning of the array | The binary search correctly shifts the search window to the right half once the initial duplicate pairing invariant fails. |
| Single element located at the very end of the array | The algorithm exhausts the left partitions and converges on the final index as the low and high pointers meet. |
| Arrays with negative numbers and mixed zero values | The approach relies on relative index parity and value comparisons rather than absolute magnitudes, working seamlessly with negatives. |
| Extremely large arrays approaching memory or performance limits | Operating in logarithmic time complexity and constant space complexity prevents stack overflow and timeout issues. |
| Even-length array violating the problem constraints | The problem statement guarantees a valid configuration with an odd length, so index out of bounds is prevented by design. |