File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int [] singleNumber (int [] nums ) {
3
+ /*
4
+ We keep xoring elements in the array and since 0^N = N and N^N = 0, then result would have a^b since a
5
+ and b occur just once. So, we could also be sure that there would definitely be a 1 in the result as
6
+ a and b are different numbers. So, we have to find that last set bit, which we can find by this formula
7
+ diff = diff & ~(diff - 1), this would make the last set bit as 1 and rest 0. So, this is the bit which
8
+ differs in a and b. So, we loop throught the array again and check the elements having this set bit
9
+ and this bit unset and xor in those two halves to find a and b.
10
+ */
3
11
// Pass 1 :
4
12
// Get the XOR of the two numbers we need to find
5
13
int diff = 0 ;
You can’t perform that action at this time.
0 commit comments