class Solution {
public:
    int singleNumber(int* A, int n) {
        if (!A || n == 0) return 0;
        int result = 0;
        int N = 3;
        if (N & 1)
        {
            for (int i = 0; i < 32; ++i)
            {
                int bit = 0;
                for (int j = 0; j < n; ++j)
                {
                    bit += (A[j] >> i) & 1;
                }
                result += (bit % N) << i;
            }
        }
        else
        {
            for (int i = 0; i < n; ++i)
            {
                result ^= A[i];
            }
        }
        return result;
    }
};