XOR without bitmask

int Solution::singleNumber(const vector<int> &A) {
    int first = 0;
    int second = 0;
    for(auto n:A){
        first = (first ^ n) & ~second;
        second = (second ^ n) & ~first;
    return first;


Can you please explain what you are doing here?


Please explain your code, I have searched on youtube somebody else has also solved like this but I am not able to understand, if you could explain it would be a great help.


Here, He counts no. of set bit at ith position for whole array,by taking %3 we get the bit which does not repeat(rest come 3 times).