Unique number three

Your approach works fine for positive numbers but what about if i give negative numbers as input? It is not giving correct ans.
For example
let n=10
-2,-2,1,1,-3,1,-3,-3,-4,-2

the output must be -4 but it is showing 0.
Suggest correct approach to solve this question.

@yath17 In the approach taught in the video, we are doing shift operations on the given numbers. Remember that The left shift and right shift operators should not be used for negative numbers.Left shifting and right shifting negative numbers always introduces a leftmost set bit(signed bit). Due this problem arises as this can never turn to 0 by right shifting or left shifting. The result of is undefined behaviour if any of the operands is a negative number. Due to this reason the code is failing for negative numbers.

One approach to solve the problem by avoiding the shifting operations on the original number:
(Please dry run with any sample case to understand the code. The approach is somewhat similar to the previous one)
//Here we are reserving the 32 bits space for the final resulting number and setting the bits accordingly
//Note that here we are not performing shifting operation on the numbers stored in the vector
int uniquenumber(vector< int >& nums) {

    int res = 0;
    int len=nums.size();
    for(int i=0; i<32; i++)
    {
        int mask = (1 << i);
        int count = 0;
        for(int j=0; j<len; j++)
            if(mask&nums[j]) count++;
        if(count%3)
            res |= (1<<i);
    }
    return res;
}

Hope this helps :slightly_smiling_face: