import java.util.*;

/**
 * Given an array of integers, every element appears three times except for
 * one. Find that single one.
 * 
 * Note:
 * Your algorithm should have a linear runtime complexity. Could you implement
 * it without using extra memory?
 * 
 * Tags: Bit Manipulation
 */
class SingleNum2 {
    
    public static void main(String[] args) {
        int[] A = {1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7};
        System.out.println(singleNum(A));
    }
    
    /**
     * Use ones to store those nums only appeared once
     * twos to store those nums appeared twice
     */
    public static int singleNum(int[] A) {
        int ones = 0, twos = 0;
        for (int i = 0; i < A.length; i++) {
            ones = (ones ^ A[i]) & ~twos; // in ones not in twos in case the A[i] was in twos.
            twos = (twos ^ A[i]) & ~ones; // in twos not in ones. Rule out the new A[i].
        }
        return ones; // only appeared once
    }
}

@@ -725,191 +725,407 @@
 ) %7B%0A
-            ones = (ones %5E A%5Bi%5D) & ~twos; // in ones not in twos in case the A%5Bi%5D was in twos.%0A            twos = (twos %5E A%5Bi%5D) & ~ones; // in twos not in ones. Rule out the new A%5Bi%5D.
+%09%09%09// New A%5Bi%5D: %0A%09%09%09// case 1 - never show up;%0A%09%09%09// case 2 - show up once already;%0A%09%09%09// case 3 show up twice already%0A%09%09%09%0A%09%09%09// %5E eliminate case 2, need ~twos to elimi1 case 3.%0A            ones = (ones %5E A%5Bi%5D) & ~twos; %0A%09%09%09%0A%09%09%09// %5E eliminate case 3, need ~ones(updated) to elimi1 case 1. Note if A%5Bi%5D was in ones, it has been removed from the updated ones.%0A            twos = (twos %5E A%5Bi%5D) & ~ones;
 %0A   

Update comments.