Single Number II

Given an array of integers, every element appears thrice except for one which occurs once.

Find that element which does not appear thrice.

Note: Your algorithm should have a linear runtime complexity.

Could you implement it without using extra memory?

Example :

Input : [1, 2, 4, 3, 3, 2, 2, 3, 1, 1]
Output : 4
Method:

There are 32 bits in an integer, if a number appears thrice, then the bits for that number should appear as a multiple of 3 as well.

Solution:

Time: O(n)
Space: O(1)

public class Solution {
    // DO NOT MODIFY THE LIST. IT IS READ ONLY
    public int singleNumber(final List<Integer> A) {
        int totalBits = 32;
        int result = 0;
        int currentBit = 1;
        while (totalBits >= 0) {
            int count = 0;
            for (int n : A) {
                if ((currentBit & n) >= 1) {
                    count ++;
                }
            }
            if (count % 3 == 1) {
                result |= currentBit;
            }
            currentBit <<= 1;
            totalBits --;
        }
        return result;
    }
}