import java.util.HashMap;

/**
 * Problem: #137 Single Number II
 *
 * Idea:
 *
 * Solutions:
 *  singleNumberSet: using a hash set to store unique characters found.
 *
 * Note: #canbefaster
 */
public class SingleNumberII {
    public int singleNumber(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                if (map.get(num) == 2) {
                    map.remove(num);
                } else {
                    map.put(num, 2);
                }
            } else {
                map.put(num, 1);
            }
        }
        return (Integer) map.keySet().toArray()[0];
    }
}

@@ -364,81 +364,71 @@
     
-if (map.
+Integer 
 co
+u
 nt
-ainsKey(num)) %7B%0A                if (map.get(num) == 2) %7B%0A    
+ = map.get(num);%0A            if (count == null) %7B%0A
     
@@ -447,25 +447,21 @@
 map.
-remove(num);%0A    
+put(num, 1);%0A
     
@@ -467,38 +467,50 @@
          %7D else 
-%7B%0A    
+if (count == 2) %7B%0A
                 
@@ -517,38 +517,20 @@
 map.
-put(num, 2);%0A                %7D
+remove(num);
 %0A   
@@ -568,33 +568,33 @@
    map.put(num, 
-1
+2
 );%0A            %7D

Add new solution