# Number not in pair using XOR

  • Suppose we have an array containing numbers in pairs which are stored randomly anywhere in the array & but there is one number which is not in pair.

  • We will use the XOR operator to get the number which is not in pair

# Source Code - C++

#include <iostream>
using namespace std;

//Function to find number not in pair
int findNum(int A[],int n)
{

int num=0,i;
for(i=0;i<n;i++)
num=num^A[i];

return num;
}
//Main Function
int main()
{
int n,i,ans;
cout<<"Enter number of elements\n";
cin>>n;
int A[n];
cout<<"Enter elements\n";
for(i=0;i<n;i++)
cin>>A[i];

ans=findNum(A,n);
cout<<ans;
return 0;
}

Time Complexity : O(n)

Learn More


  • Number not in pair using XOR