BiruLyu
5/25/2017 - 6:36 PM

260. Single Number III.java

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;
    }
}