Single Element in a Sorted Array
Last Updated :
24 Sep, 2024
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
#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, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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]
# Driver code
arr = [1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8]
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, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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, 3, 3, 4, 5, 5, 7, 7, 8, 8];
console.log(single(arr));
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
#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, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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
# Driver code
arr = [1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8]
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, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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, 3, 3, 4, 5, 5, 7, 7, 8, 8];
console.log(single(arr));
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
#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 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() {
int arr[] = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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 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];
}
public static void main(String[] args) {
int[] arr = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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 even
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]
# Driver code
arr = [1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8]
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 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];
}
static void Main() {
int[] arr = {1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8};
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 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];
}
// Driver code
const arr = [1, 1, 3, 3, 4, 5, 5, 7, 7, 8, 8];
console.log(single(arr));
Time Complexity: O(Log n), where n is the number of elements in the array.
Auxiliary Space: O(1)
Similar Reads
Sorting all array elements except one Given an array, a positive integer, sort the array in ascending order such that the element at index K in the unsorted array stays unmoved and all other elements are sorted. Examples: Input : arr[] = {10, 4, 11, 7, 6, 20} k = 2; Output : arr[] = {4, 6, 11, 7, 10, 20} Input : arr[] = {30, 20, 10} k =
6 min read
Check if an Array is Sorted Given an array of size n, the task is to check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted.Examples: Input: arr[] = [20, 21, 45, 89, 89, 90]Output: YesInput: arr[] = [20, 20, 45, 89, 89, 90]Output: YesInput: a
9 min read
Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith
6 min read
How to sort an array in a single loop? Given an array of size N, the task is to sort this array using a single loop.How the array is sorted usually? There are many ways by which the array can be sorted in ascending order, like: Selection SortBinary SortMerge SortRadix SortInsertion Sort, etc In any of these methods, more than 1 loops is
12 min read
Count of smaller or equal elements in sorted array Given a sorted array of size n. Find a number of elements that are less than or equal to a given element. Examples: Input : arr[] = {1, 2, 4, 5, 8, 10} key = 9 Output : 5 Elements less than or equal to 9 are 1, 2, 4, 5, 8 therefore result will be 5. Input : arr[] = {1, 2, 2, 2, 5, 7, 9} key = 2 Outp
15+ min read