Explanation

Kindly Help me with the question Chef and Dolls Problem Code: MISSP (Beginner section) unable to understand the solution?
#include<stdio.h>
int main()
{
int tc,T;
scanf("%d",&T);
for(tc=0;tc<T;tc++)
{
int n,i,temp;
int ans=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&temp);
ans=ans^temp;
}
printf("%d\n",ans);
}
return 0;
}

When you XOR (exclusive OR) 2 same numbers the result is zero because since all bits are same, every bit in the result will be zero . for ex. (1101)^(1101) = 0000. This XOR operator is also associative as well commutative hence (a^b)^c = (a^c)^b. This means if there are chain of elements XORing each other they can be rearrange in any order i.e 2^4^2 = 2^2^4.In the question every element has a pair except one.If XOR every element with each other then the same element will become zero since XORing with self is zero and what XORing with zero is the number itself.Hence ans is initially zero and then contains the element that does not have any pairs.

//