Bitwise unique number 2

not getting correct output in unique num2 problem bit wise

@guptashubham210a you have implemented the same logic as video or some other?

Step 1: XOR all the numbers. You will be left over with the XOR result of the two unique numbers.
Step 2: Find the position of the rightmost set bit of the result. Say it is i.
Step 3: Pair the given numbers according to the set bits at ith position.(XOR all the numbers whose i’th bit is set) This will give you one unique number.
Step 4: XOR the result of Step 1 with Step 3 and you will get the second unique number.

Say the the numbers are : 2 1 3 5 1 6 3 2
Step 1: Will give the XOR result of 5 and 6 ie 5^6 . It will be 101 ^ 110 = 011
Step 2: So the rightmost set bit of result is at i=0 from left.
Step 3: So pair all the numbers whose last or 0th bit is set.So result will be 1^3^5^1^3= 5 So 5 is one unique number.
Step 4: (Step 1)^(Step 3)= 5^6^5=6 which is the second unique number.

Explained here Unique number 2

same logic but not getting correct output>pls check my code.

ok wait let me check

@guptashubham210a one small mistake corrected and commented


dont forget to hit like and mark resolved if cleared :smiley:

1 Like

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.