Single Element in a Sorted Array
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.
#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;
}
#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;
}
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));
}
}
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))
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));
}
}
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));
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.
#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;
}
#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;
}
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));
}
}
# 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))
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));
}
}
// 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));
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:
#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;
}
#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;
}
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));
}
}
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))
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));
}
}
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));
Output
4
Time Complexity: O(Log n), where n is the number of elements in the array.
Auxiliary Space: O(1)