15% off all plans & roadmaps Use code
See plansLeetCode 540 · Solution & editorial
You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
Single Element in a Sorted Array is a Binary Search Patterns problem, in the On Sorted Array/List subpattern. The pattern covers standard binary search, rotated arrays, first/last occurrence, and search on answer space
Master standard binary search on sorted arrays. Learn search insert position, sqrt calculation, and 2D matrix search.
The reference solution runs in O(logn) time and O(1) space.
Single Element in a Sorted Array has been reported in interviews at Amazon, Facebook, Twitter.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Single Element in a Sorted Array is a Binary Search Patterns problem, in the On Sorted Array/List subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.