"""
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly
 twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
ANSWER:
By getting XOR of all number, we have XOR of two need-to-find numbers. Of course, there are certain bit of this result
is 1, then base on this bit to split the array to two list, one is set and one is unset at this bit
"""
class Solution(object):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    def singleNumber(self, nums):
        all_xor = 0
        for n in nums:
            all_xor ^= n
        i = 0
        while (all_xor >> i) & 1 == 0:
            i += 1
        a = b = 0
        for n in nums:
            if (n>>i)&1:
                a ^= n
            else:
                b ^= n
        return [a, b]


if __name__ == '__main__':
    print Solution().singleNumber([6,2,2,3,3,4,5,5,4,1])