260. single number iii

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.

Example:

1
2
Input:  [1,2,1,3,2,5]
Output: [3,5]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class  {
public int[] singleNumber(int[] array) {
int[] arr = new int[2];
if (array == null || array.length == 0) return arr;
int x1orx2 = 0;
for (int i : array) {
x1orx2 ^= i;
}
int last1Bit = x1orx2 - (x1orx2 & (x1orx2 - 1));
int odd = 0;
int even = 0;
for (int i :array) {
if ((last1Bit & i) == 0) {
odd ^= i;
} else {
even ^= i;
}
}
arr[0] = odd;
arr[1] = even;

return arr;
}
}