Using XOR soln C++


#1

Main idea behind using XOR is:-
a) XOR of a number with itself is 0.
b) XOR of a number with 0 is number itself.

int Solution::singleNumber(const vector &A) {
int res=A[0];
for(int i=1;i<A.size();i++)
{
res=res^A[i];
}
return res;
}


#2

what if the input is 1 2 2 2
then res will return 3 and not 1


#3

its not relative to the queston but what changes will u do to handle the above testcase