How is XOR able to give unique number from multiple inputs?

from given test case, i.e. 3, 2, 3, 5, 6,5, 2

ans = 0;

1st run : 0^3 = 3;
2nd run: 3^2 = 1;
3rd run: 1^3 = 2;
4th run: 2^5 = 7;
5th run: 7^6 = 1;
6th run: 1^5 = 4;
7th run: 4^2 = 6;

In the end, we came to the resultant answer, I get that. But I don’t understand why is if intermediate ans are different and it doesn’t even know if the previous number was repeated.

Suppose there are 6 digits only, the output is outside the input given, the output is XOR of remaining two unique numbers.

I am unable to understand the logic beyond two same numbers give 0 as output for their XOR.

This is because XOR only cares about bits. So intermediate state can be anything, but when you add a number x, bits corresponding to it get added. So when you again add x, buts corresponding to it will get removed, no matter what.

so XOR can only be used for finding unique number in the array and not to check how many numbers are occuring only once?

Yes it only finds the unique number if all the other numbers have even occurences.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.