Single Element in a Sorted Array
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] <= 105
Explanation
The key signal is index parity.
Before the single element, pairs are aligned as (even, odd): for example (0,1), (2,3), (4,5).
After the single element, this alignment shifts by one, so pairs become (odd, even).
That gives us a boolean test to_the_left(idx):
if it returns True, the single element is at idx or somewhere to its left.
if it returns False, the single element is strictly to the right.
For an odd index idx, compare with the previous element:
if nums[idx] != nums[idx - 1], the shift has already happened, so the single element is on the left.
For an even index idx, compare with the next element:
if nums[idx] != nums[idx + 1], normal pairing is broken here, so the single element is on the left.
The only boundary case is when idx is the last index.
Then there is no idx + 1, and the answer must be at or to the left, so return True.

Implementation
def singleNonDuplicate(self, nums):
def to_the_left(idx):
if (idx == len(nums)-1):
return True
elif (idx % 2): # odd
return nums[idx] != nums[idx-1]
else: # even
return nums[idx] != nums[idx+1]
left, right, ans = 0, len(nums)-1, -1
while left <= right:
mid = (left + right) // 2
if to_the_left(mid):
ans = mid
right = mid - 1
else:
left = mid + 1
return nums[ans]
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorWhat are the most two important steps in writing a depth first search function? (Select 2)
Recommended Readings
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion If you prefer videos here's a video that explains recursion in a fun and easy way Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask him
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity describes how the time needed
Want a Structured Path to Master System Design Too? Don’t Miss This!