Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find single in an array of 2n+1 integer elements

  • Difficulty Level : Basic
  • Last Updated : 30 Apr, 2021

Given an array with 2n+1 integers, n elements appear twice in arbitrary places in the array and a single integer appears only once somewhere inside. Find the lonely integer with O(n) operations and O(1) extra memory.
Examples : 
 

Input : { 1, 1, 2, 2, 3, 3, 4, 4, 5}
Output : 5

Input : { 7, 9, 6, 8, 3, 7, 8, 6, 9}
Output : 3

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

The idea is to do XOR of all elements. XOR of all elements gives us the result. The idea is based on below XOR properties. 
 



  1. XOR of a number with itself is 0.
  2. XOR of a number with 0 is the number.

 

C++




// CPP program to find only
// element in an array where
// every element appears twice.
#include <bits/stdc++.h>
using namespace std;
 
// Find non repeating
// number in an array
int findNonRepeating(int arr[],
                     int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 8, 3, 2, 2, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findNonRepeating(arr, n);
    return 0;
}

Java




// Java program to find only element
// in an array where every element
// appears twice.
import java.io.*;
 
class GFG
{
 
// Find non repeating
// number in an array
static int findNonRepeating(int []arr,
                            int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
    return res;
}
 
// Driver Code
static public void main (String[] args)
{
int []arr = {3, 8, 3, 2, 2, 1, 1};
int n = arr.length;
System.out.println(findNonRepeating(arr, n));
}
}
 
// This code is contributed by vt_m.

Python




# Find non repeating
# number in an array
 
def findNonRepeating(a, n):
    res = 0
     
    # XOR of all numbers
    for i in range(n):
        res ^= a[i]
    return res
     
# Driver code
a = [ 3, 8, 3, 2, 2, 1, 1 ]
n = len(a)
 
print findNonRepeating(a, n)
 
# This code is contributed
# by 'Striver'.

C#




// C# program to find only element
// in an array where every element
// appears twice.
using System;
 
class GFG
{
 
// Find non repeating number in an array
static int findNonRepeating(int []arr,
                            int n)
{
    int res = 0;
    for (int i = 0; i < n; i++)
        res = res ^ arr[i];
    return res;
}
 
// Driver Code
static public void Main (String []args)
{
int []arr = {3, 8, 3, 2, 2, 1, 1};
int n = arr.Length;
Console.WriteLine(findNonRepeating(arr,
                                   n));
}
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to find only
// element in an array where
// every element appears twice.
 
// Find non repeating number
// in an array
function findNonRepeating($arr, $n)
{
    $res = 0;
    for ($i = 0; $i < $n; $i++)
        $res = $res ^ $arr[$i];
    return $res;
}
 
// Driver Code
$arr = array( 3, 8, 3, 2, 2, 1, 1 );
$n = sizeof($arr);
echo(findNonRepeating($arr, $n));
 
// This code is contributed by Ajit.
?>

Javascript




<script>
 
// Javascript program to find only
// element in an array where
// every element appears twice.
 
// Find non repeating
// number in an array
function findNonRepeating(arr, n)
{
    let res = 0;
    for (let i = 0; i < n; i++)
        res = res ^ arr[i];
    return res;
}
 
// Driver Code
    let arr = [ 3, 8, 3, 2, 2, 1, 1 ];
    let n = arr.length;
    document.write(findNonRepeating(arr, n));
 
</script>

Output :  

8

Time Complexity: O(n). 
Space Complexity: O(1).
This article is contributed by ASIPU PAWAN KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!