Single Element in a Sorted Array (original) (raw)

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.

Try It Yourselfredirect icon

Table of Content

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

#include #include using namespace std;

int single(vector& 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 arr = {1, 1, 2, 2, 3, 4, 4}; cout << single(arr); return 0; }

C

#include <stdio.h>

int single(int arr[], int n) {

// 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() { int arr[] = {1, 1, 2, 2, 3, 4, 4}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d", single(arr, n)); return 0; }

Java

class GfG {

static int single(int[] arr) {
    int n = arr.length;

    // 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];
}

public static void main(String[] args) {
    int[] arr = {1, 1, 2, 2, 3, 4, 4};
    System.out.println(single(arr));
}

}

Python

def single(arr): n = len(arr)

# Since every other element appears twice,
# the single element must be at an odd
# position
for i in range(0, n - 1, 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]

if name == "main": arr = [1, 1, 2, 2, 3, 4, 4] print(single(arr))

C#

using System;

class GfG { static int single(int[] arr) { int n = arr.Length;

    // 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];
}

static void Main() {
    int[] arr = {1, 1, 2, 2, 3, 4, 4};
    Console.WriteLine(single(arr));
}

}

JavaScript

function single(arr) { const n = arr.length;

// Since every other element appears twice,
// the single element must be at an odd
// position
for (let 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];

}

// Driver code const arr = [1, 1, 2, 2, 3, 4, 4]; console.log(single(arr));

`

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

C++ `

#include #include using namespace std;

// A XOR based function to find // the element that appears only once int single(vector& arr) {

int XOR = 0;
for (int i = 0; i < arr.size(); i++) {
    XOR ^= arr[i];
}
return XOR;

}

int main() { vector arr = { 1, 1, 2, 2, 3, 4, 4}; cout << single(arr) << "\n"; return 0; }

C

#include <stdio.h>

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

int main() { int arr[] = {1, 1, 2, 2, 3, 4, 4}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n", single(arr, n)); return 0; }

Java

class GfG {

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

public static void main(String[] args) {
    int[] arr = {1, 1, 2, 2, 3, 4, 4};
    System.out.println(single(arr));
}

}

Python

A XOR based function to find

the element that appears only once

def single(arr): XOR = 0 for i in arr: XOR ^= i return XOR

if name == "main": arr = [1, 1, 2, 2, 3, 4, 4] print(single(arr))

C#

using System;

class GfG {

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

static void Main() {
    int[] arr = {1, 1, 2, 2, 3, 4, 4};
    Console.WriteLine(single(arr));
}

}

JavaScript

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

// Driver code const arr = [1, 1, 2, 2, 3, 4, 4]; console.log(single(arr));

`

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

#include #include using namespace std;

int single(vector& arr) { int n = arr.size(); int lo = 0, hi = n - 1;

while (lo < hi) {
    int mid = lo + (hi - lo) / 2;
    
    // Ensure mid is odd
    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 arr = {1, 1, 2, 2, 3, 4, 4}; cout << single(arr) << "\n"; return 0; }

C

#include <stdio.h>

int single(int arr[], int n) { int lo = 0, hi = n - 1;

while (lo < hi) {
    int mid = lo + (hi - lo) / 2;
    
    // Ensure mid is odd
    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() { int arr[] = {1, 1, 2, 2, 3, 4, 4}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n", single(arr, n)); return 0; }

Java

class GfG {

static int single(int[] arr) {
    int lo = 0, hi = arr.length - 1;
    
    while (lo < hi) {
        int mid = lo + (hi - lo) / 2;
        
        // Ensure mid is odd
        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];
}

public static void main(String[] args) {
    int[] arr = {1, 1, 2, 2, 3, 4, 4};
    System.out.println(single(arr));
}

}

Python

def single(arr): lo, hi = 0, len(arr) - 1

while lo < hi:
    mid = lo + (hi - lo) // 2
    
    # Ensure mid is odd
    if mid % 2 == 1:
        mid -= 1
    
    # 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]

if name == "main": arr = [1, 1, 2, 2, 3, 4, 4] print(single(arr))

C#

using System;

class GfG { static int single(int[] arr) { int lo = 0, hi = arr.Length - 1;

    while (lo < hi) {
        int mid = lo + (hi - lo) / 2;
        
        // Ensure mid is odd
        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];
}

static void Main() {
    int[] arr = {1, 1, 2, 2, 3, 4, 4};
    Console.WriteLine(single(arr));
}

}

JavaScript

function single(arr) { let lo = 0, hi = arr.length - 1;

while (lo < hi) {
    let mid = lo + Math.floor((hi - lo) / 2);
    
    // Ensure mid is odd
    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];

}

// Driver code const arr = [1, 1, 2, 2, 3, 4, 4]; console.log(single(arr));

`