Why do we take res=0 and make the first value XOR with zero?. Is there any logic behind it?
I got confused as why we are taking res=0 in this code?
@subham221 yes, in zero there are no setbits, if we take any other number we will have setbits, that will interact with our mask, and change the result.
If this resolves your query mark it resolved.
You didn’t get me!! I am asking we take the result=0 in the beginning (the first most xor of the first number in the array is done with zero). Why??
@subham221 cause taking xor of any number with zero does not change that number.
Suppose we need to take xor of elements in an array, we can do two things,
first
res=0;
for(int i=0;i<n;i++)
res=res^arr[i];
or
res= a[0]
for(int i=1;i<n;i++)
res=res^a[i];
After the loop ends both res will have same value
if this resolves your doubt mark it as resolved.