Four multiple states situation it would be much easier to use n bits to represent at most n-1 appearing times, it requires more space but avoiding headache.

I suggest that returning a | b can be replaced by returning b. Because a should be equal to zero now. After making this tiny change, the code still makes sense in testing and runs faster. The question is if there exists a general solution for the problem that each number can appear k times. In this case, concluding the law can be very hard.

we use state (0,0) (0,1) (1,1) to represent we've met bit 1 for 0,1,2 times.
So if the incoming bit is 0, state keeps unchanged while for 1 we come to the next one.
It's brilliant!

I think the last 6 line should be changed to if(digits[i]%3>0). Otherwise, there will be not covered the case that the exceptional number appears in TWICE, such as {1,3,4,1,3,4,5,5,1,3,4}

Thanks for the answer. Although this is not the optimal one (it takes O(32n) complexity because of the outer loop), but it is easy to understand and it is more likely to be the solution that one can quickly come up during an interview.