Search⌘ K
AI Features

Solution: Single Element in a Sorted Array

Explore how to efficiently identify the single non-duplicate element in a sorted array using a modified binary search. Learn pointer management and indexing tricks that enable an optimal O(log n) time and constant space solution for this common coding interview pattern.

Statement

You are given a sorted array of integers, nums, where all integers appear twice except for one. Your task is to find and return the single integer that appears only once.

The solution should have a time complexity of O(logn)O(\log n) or better and a space complexity of O(1)O(1).

Constraints:

  • 11 \leq nums.length 103\leq 10^3 ...