Here is the implementation:
function singleNumber(nums):
xorAll = 0
for num in nums:
xorAll = xorAll XOR num
// xorAll = a XOR b
// Find rightmost set bit
rightmostBit = xorAll & (-xorAll)
a = 0, b = 0
for num in nums:
if (num & rightmostBit) != 0:
a = a XOR num
else:
b = b XOR num
return [a, b]
time, space (excluding input). Two passes through the array.