Unique Numbers - 2

I didn’t understand the coding part of this topic

@jais_rashi There is already a discussion video of this question. Please check it in your course playlist.

Follow the approach as taught in the course:
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.

Hope this helps. :slightly_smiling_face:

1 Like

Logic samjh aa gya…not getting the implementation prt


Refer this. Dry run with the sample case to understand.

Got it Thankyou so much

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.