Back to problems Sign in to unlock
Leetcode
Medium
Arrays
Binary Search
Bit Manipulation
Google
Amazon
Single Element In A Sorted Array
Find the one element that appears exactly once in a sorted array where every other element appears twice.
Acceptance 100%
Problem Statement
Problem
You are given a sorted integer array in which every value appears exactly twice, except for one value that appears only once.
Return the single value that appears once.
Your solution should run efficiently, taking advantage of the sorted order rather than scanning the array linearly.
Input Format
- A sorted integer array
numsof length at least 1. - Every element appears exactly twice except one element that appears once.
Output Format
- Return the unique element that appears only one time.
Constraints
- The array is sorted in non-decreasing order.
- All values except one appear exactly twice.
- Exactly one value appears once.
- Aim for better than linear time.
Hints
- In a correctly paired sorted array, duplicate elements occupy neighboring positions.
- Consider how the position of the unique element affects the pairing structure on the left and right sides.
- Binary search can be used to check whether the current midpoint belongs to a valid pair structure.
Input Format
nums: a sorted integer array- Exactly one number appears once; all others appear twice
Output Format
- Return the single integer that does not have a duplicate
Constraints
- Sorted array
- All numbers except one appear twice
- Exactly one number appears once
- Prefer time and extra space
Examples
Sample cases returned by the problem API.
Example 1
Input
nums = [1,1,2,3,3,4,4,8,8]
Output
2
Explanation
Every number appears twice except 2, so 2 is the single element.
Example 2
Input
nums = [3,3,7,7,10,11,11]
Output
10
Explanation
All values are paired except 10.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.
Guided hints
Editorial and discussion links
Concept map and variants
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.