# Single Number II

## Improved version of Single Number

`xor`

can not work if the number appears 3 times.

### Yet another tricky bit manipulation

[1,2,3,5,1,2,3,1,2,3] in binary form of

```
0 0 0 1
0 0 1 0
0 0 1 1
0 1 1 0
0 0 0 1
0 0 1 0
0 0 1 1
0 0 0 1
0 0 1 0
0 0 1 1
count 0 and 1
0 1 7 6
```

then remove the number which `mod 3 == 0`

in 0 1 7 6 to 0. the number becomes 0 1 1 0 in decimal form (5).

use an array to count how many `1`

appears, the final bit depends on the count number.

### Source code *Read on Github*

```
1 public class Solution {
2 public int singleNumber(int[] A) {
3 // Note: The Solution object is instantiated only once and is reused by each test case.
4
5 int[] count = new int[Integer.SIZE];
6 int[] bit = new int[Integer.SIZE];
7
8 Arrays.fill(count, 0);
9 Arrays.fill(bit, 0);
10
11 for(int a : A){
12
13 for(int b = 0; b < Integer.SIZE; b++){
14 int x = a >>> b & 1;
15 bit[b] |= x;
16
17 if(x == 1)
18 count[b]++;
19 }
20
21 }
22
23 int s = 0;
24 for(int b = 0; b < Integer.SIZE; b++ ){
25 if (count[b] % 3 != 0)
26 s |= bit[b] << b;
27 }
28
29 return s;
30
31 }
32 }
```