
Single Element in a Sorted Array (medium)
Problem Statement
Given a sorted integer array nums in which each element appears exactly twice, and only 1 element appears exactly once, return the single element that appears only once.
Note: You must solve the problem in O(log n) time and O(1) space.
Examples
-
Example 1:
- Input: nums =
[4,4,6,6,10,11,11] - Expected Output: 10
- Justification: Here, 10 is the only number that doesn't have a pair.
- Input: nums =
-
Example 2:
- Input: nums =
[1,1,2,3,3,4,4] - Expected Output: 2
- Justification: 2 is unique as it does not repeat, making it a single element.
- Input: nums =
-
Example 3:
- Input: nums =
[5,7,7,8,8,10,10] - Expected Output: 5
- Justification: 5 stands out as it's the only element without a duplicate in the sorted array.
- Input: nums =
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory