VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-element-that-appears-once-in-a-sorted-array/

⇱ Single Element in a Sorted Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Single Element in a Sorted Array

Last Updated : 5 Aug, 2025

Given a sorted array arr[] where every element appears exactly twice except one element that appears only once, find that single element.

Examples: 

Input: arr[] = [1, 1, 2, 2, 3, 4, 4]
Output: 3
Explanation: All numbers except 3 occur twice in the array.

Input: arr[] = [1, 1, 3, 3, 4, 4, 5, 5, 7, 7, 8]
Output: 8
Explanation: All numbers except 8 occur twice in the array.

[Naive Approach 1] By comparing adjacent elements - O(n) Time and O(1) Space:

The idea behind this approach is to check every adjacent element in order to find if there exists its pair or not as the array is sorted.

  • The single element must appear at an odd position (or even index) because every other element appears twice.
  • One by one check all odd postilions (or even indexes) and if we find an element which is not same as next, we return the element.

Output
3

[Naive Approach 2] Using Bitwise XOR - O(n) Time and O(1) Space:

The idea behind this approach is to use bitwise XOR between pair of elements, as XOR of two same numbers is always equal to 0.

We can use the properties of XOR (a ^ a = 0 & a ^ 0 = a) to find the element that occurs once. The idea is to find the XOR of the complete array, so all the elements which occur twice will have their XOR = 0 and the XOR of the array will be the required answer.


Output
3

[Expected Approach] Using Binary Search - O(log n) Time and O(1) Space:

The idea is to use Binary Search. Below is an observation on the input array. 
All elements before the element that occurs once have the first occurrence at even index (0, 2, ..) and the next occurrence at odd index (1, 3, ...). And all elements after the element that occurs once have the first occurrence at an odd index and the next occurrence at an even index.

  • Find the middle index, say 'mid'. If mid is odd, then reduce it by 1 to make it even
  • If 'arr[mid] and arr[mid+1] are same, then the single element must be on the right side
  • Else single element must be on the left side.

Output
3
Comment