Skip to main content

Single Element in a Sorted Array - Solution & Explanation

MediumArrayBinary Search15 min readAsked at: Amazon, Microsoft, Goldman Sachs +16
Practice this problem

Problem Statement

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 <= 105
  • 0 <= nums[i] <= 105

Approach Overview

Problem Overview: You are given a sorted array where every element appears exactly twice except one element that appears once. The task is to return that single element. The sorted property creates a predictable pairing pattern that allows more efficient solutions than a full scan.

Approach 1: Bitwise XOR (O(n) time, O(1) space)

The simplest solution relies on the XOR property: a ^ a = 0 and a ^ 0 = a. Iterate through the array and XOR every value with an accumulator. Since every duplicated element cancels itself out, the final value left in the accumulator is the single element. This approach ignores the sorted nature of the array and works for any array where every number appears twice except one. Implementation is extremely short and constant space, but the runtime is linear because you must scan every element.

Approach 2: Binary Search (O(log n) time, O(1) space)

The optimal solution leverages the sorted structure of the array together with binary search. In a correctly paired array, elements appear in pairs where the first index is even and the second index is odd (e.g., [a,a,b,b,c,c]). Once the single element appears, this pairing pattern breaks. During binary search, check whether mid forms a valid pair with its neighbor. If mid is even and nums[mid] == nums[mid+1], the single element lies to the right; otherwise it lies to the left. If mid is odd, compare with nums[mid-1]. Each step discards half of the search space, reducing the runtime to logarithmic time.

The key insight is that the single element shifts the index parity of every pair after it. Binary search detects where that shift occurs. Because the array is sorted and duplicates are adjacent, each comparison immediately tells you which half of the array still follows the valid pairing structure.

Recommended for interviews: The binary search solution is what interviewers typically expect because it exploits the sorted property and achieves O(log n) time. The XOR approach is still useful to mention first—it demonstrates awareness of bitwise tricks and works in the general case—but the optimized binary search shows stronger algorithmic reasoning and pattern recognition.

Approach 1: Binary Search

This approach leverages the sorted property of the array to perform a binary search, achieving a time complexity of O(log n). The key observation is that elements are paired, except for the single unique element. Thus, we can use the middle index to determine if the unique element is in the left or right half of the array based on the pairing pattern.

The binary search here divides the array into two halves. If the middle element is equal to the element next to it and its index is even, it implies the single element is in the second half. Otherwise, it's in the first half. This process continues until left equals right, pointing to the single element.

Code

C

C++

Java

Python

C#

JavaScript

Go

TypeScript

Rust

Complexity

Time Complexity: O(log n)
Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Bitwise XOR

This approach exploits the properties of the XOR operation: a ^ a = 0 and a ^ 0 = a. We can XOR all elements, and since duplicates XOR to 0, only the unique element will remain.

The XOR operation cancels out all duplicates, leaving only the single unique element. This takes advantage of the property that x ^ x = 0 and x ^ 0 = x.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(1)

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Binary Search

Time Complexity: O(log n)
Space Complexity: O(1)

Bitwise XOR

Time Complexity: O(n)
Space Complexity: O(1)

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Bitwise XORO(n)O(1)When the array may not be sorted or you want the simplest constant-space scan
Binary SearchO(log n)O(1)Best choice when the array is sorted and duplicates appear in pairs

Video Solution

BS-8. Single Element in Sorted Array • take U forward • 407,650 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Single Element in a Sorted Array easy or hard?
Single Element in a Sorted Array is rated Medium difficulty. The challenge is recognizing how the sorted pairing pattern changes after the single element and using that observation to adapt binary search.
Single Element in a Sorted Array Python/Java solution
Python and Java implementations usually follow the same binary search pattern: compute mid, check whether the pair structure is valid, and move the search boundaries accordingly. Both languages achieve O(log n) time and constant space with this approach.
How to solve Single Element in a Sorted Array in O(log n)?
Use binary search and observe the index parity of pairs. In a valid prefix of the array, the first occurrence of a pair appears at an even index and the second at an odd index. When this pattern breaks, the single element lies on that side of the array, allowing you to discard half the search space each step.
What is the best approach for Single Element in a Sorted Array?
Binary search is the best approach because the array is already sorted and duplicates appear in pairs. By checking whether the pairing pattern holds at the midpoint, you can discard half of the array each step. This reduces the runtime to O(log n) while using O(1) extra space.
Is Single Element in a Sorted Array asked at Google/Amazon/Meta?
Single Element in a Sorted Array frequently appears in coding interviews at companies like Google, Amazon, and Meta because it tests binary search variations and reasoning about sorted data structures. Interviewers often expect the O(log n) binary search solution rather than a linear scan.
What data structure is used in Single Element in a Sorted Array?
The problem primarily uses arrays along with the binary search algorithm. Some implementations also use bitwise XOR operations to cancel duplicate values when solving the problem with a linear scan.
What is the time complexity of Single Element in a Sorted Array?
The optimal binary search solution runs in O(log n) time with O(1) space. A simpler XOR-based scan takes O(n) time and O(1) space because it processes every element in the array.

Ready to solve this problem?

Practice Single Element in a Sorted Array with our built-in code editor and test cases.

Practice on FleetCode