Answers for "Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2"

C++
0

Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int arr[n];
    cout<<"enter the elements of the array:"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int res=0;
    int x=0;
    int y=0;
    int setbit;
    for(int i=0;i<n;i++)
    {
        res=res^arr[i];
    }
    setbit=res&(~(res)+1);
    for(int i=0;i<n;i++)
    {
        if(arr[i]&setbit)   //xor the first set
        {
            x=x^arr[i];
        }
        else            //xor the second set
        {
            y=y^arr[i];
        }
    }
    cout<<x<<" "<<y<<endl;
    return 0;
}
Posted by: Guest on June-02-2021
0

Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2

Time Complexity:O(n) and space complexity:o(1)
Posted by: Guest on June-13-2021

Code answers related to "Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2"

Browse Popular Code Answers by Language