Skip to content

Instantly share code, notes, and snippets.

@NAVNEETOJHA
Created June 23, 2020 02:12
Show Gist options
  • Save NAVNEETOJHA/91e4e049bfe415775ebce47848f04ecc to your computer and use it in GitHub Desktop.
Save NAVNEETOJHA/91e4e049bfe415775ebce47848f04ecc to your computer and use it in GitHub Desktop.
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