Skip to content

Instantly share code, notes, and snippets.

@BiruLyu
Created May 25, 2017 18:36
Show Gist options
  • Select an option

  • Save BiruLyu/643ced5fa530b5f5fb7dabfbdc615026 to your computer and use it in GitHub Desktop.

Select an option

Save BiruLyu/643ced5fa530b5f5fb7dabfbdc615026 to your computer and use it in GitHub Desktop.
public class Solution {
public int[] singleNumber(int[] nums) {
// Get the XOR of the two numbers we need to find
int xortwo = 0;
for(int num : nums){
xortwo ^= num;
}
// Get its last set bit
int mask = xortwo & ( -xortwo );
int[] res = {0, 0};
for(int num : nums){
if((num & mask) == 0){
res[0] ^= num;
} else {
res[1] ^= num;
}
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment