In an array except three numbers all numbers occur odd number of times. How to find all three numbers occurring even number of times in an array?
I can get till a point where I have xor of all three even occurring numbers. How to get the three numbers from that xor value? My approach till now:
#include <iostream>
#include <set>
using namespace std;
int main() {
// Find the 3 numbers occuring even times in an array
int arr[] = {1,6,2,88,34,4,98,25,61,7,2,78,2,78,8,25,9,34,56,331};
int a = arr[0];
for (int i=1;i<20;i++)
a ^= arr[i];
set<int> myset;
myset.insert(arr,arr+20);
set<int>::iterator it;
for (it=myset.begin(); it!=myset.end(); ++it)
a ^= *it;
cout<<a<<endl; // a stores xor of the three numbers occuring even number of times, i.e., (25^78^34)=117
return 0;
}
Note: Hashtable and sorting should not be used. The solution should be in O(n) time.