Skip to content

Commit bfda77b

Browse files
committed
updated explanation
1 parent 50a3564 commit bfda77b

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

bit_manipulation/singleNumber3.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
class Solution {
22
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+
*/
311
// Pass 1 :
412
// Get the XOR of the two numbers we need to find
513
int diff = 0;

0 commit comments

Comments
 (0)