Created
June 23, 2020 02:12
-
-
Save NAVNEETOJHA/91e4e049bfe415775ebce47848f04ecc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int singleNumber(int[] nums) { | |
int ones = 0 ; //The bits that have appeared 1st time or 4th time or 7th time ... etc [We have to return this value] | |
int twos = 0 ; // The bits that have appeared 2nd time or 5th time or 8th time .. etc. | |
int not_threes ; //‘ones’ and ‘twos’ contain those extra bits which appear 3rd time. Removethem by finding out common set bits in ‘ones’ and ‘twos’. | |
for( int x : nums ) | |
{ | |
twos |= ones & x ; //add it to twos if it exists in ones | |
ones ^= x ; //if it exists in ones, remove it, otherwise, add it | |
not_threes = ~(ones & twos) ;//if x is in ones and twos, dont add it to Threes. | |
ones &= not_threes ;//remove x from ones | |
twos &= not_threes ;//remove x from twos | |
} | |
return ones; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment