Open In App

Single Element in a Sorted Array

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a sorted array in which all elements appear twice and one element appears only once, the task is to find the element that appears once.

Examples: 

Input: arr[] = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8}
Output: 4
Explanation: All numbers except 4 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 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.

Below is the implementation of the above approach.

C++
#include <bits/stdc++.h>
using namespace std;

int single(const vector<int>& arr) {
  
    int n = arr.size();
 
   // Since every other element appears twice,
   // the single element must be at an odd
   // position
   for (int i = 0; i < n - 1; i += 2) {
        if (arr[i] != arr[i + 1]) {
            return arr[i];
        }
    }
  
    // If no element found, the 
    // single element must be 
    // the last one
    return arr[n - 1];  
}

int main() {
    vector<int> arr = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8};
    cout << single(arr);
    return 0;
}
C Java Python C# JavaScript

Output
4

Time Complexity: O(n)
Auxiliary Space: O(1),  since no extra space has been taken.

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

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.

C++
#include <bits/stdc++.h>
using namespace std;

// A XOR based function to find
// the element that appears only once
int single(vector<int>& arr)
{
    int XOR = 0;
    for (int i = 0; i < arr.size(); i++) {
        XOR ^= arr[i];
    }
    return XOR;
}

int main()
{
    vector<int> arr = { 1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8};
    cout << single(arr) << "\n";
    return 0;
}
C Java Python C# JavaScript

Output
4

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

[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.

Below is the implementation based on the above idea: 

C++
#include <bits/stdc++.h>
using namespace std;

int single(vector<int>& arr) {
    int n = arr.size();
    int lo = 0, hi = n - 1;
    
    while (lo < hi) {
        int mid = lo + (hi - lo) / 2;
        
        // Ensure mid is even
        if (mid % 2 == 1)
            mid--;
        
        // If repeating element is at even position, 
        // then single element must be on the right side
        if (arr[mid] == arr[mid + 1]) {
            lo = mid + 2;
          
        // Else single element must be on the left  
        } else {
            hi = mid;
        }
    }
    
    return arr[lo];
}

int main() {
    vector<int> arr = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8};
    cout << single(arr) << "\n";
    return 0;
}
C Java Python C# JavaScript

Output
4

Time Complexity: O(Log n), where n is the number of elements in the array.
Auxiliary Space: O(1)



Next Article

Similar Reads

three90RightbarBannerImg