Single Number II

Source

LeetCode 137

LintCode 83

Problem

Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.

Example

Given [1,1,2,3,3,3,2,2,4,1] return 4

Solution

public int singleNumberII(int[] A) {
    int res = 0;
    for (int i = 0; i < 32; i++) {
        int sum = 0;
        for (int j = 0; j < A.length; j++) {
            sum += A[j] >> i & 1;
        }
        res |= (sum % 3) << i;
    }
    return res;
}