Array:Given an array of integers, every element appears three times except for one. Find that single one.

public int singleNumber1(int[] nums) {
        HashMap hashMap = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(nums[i])) {
                if (hashMap.get(nums[i])==2) {
                    hashMap.remove(nums[i]);
                } else {
                    hashMap.put(nums[i], hashMap.get(nums[i])+1);
                }
            } else {
                hashMap.put(nums[i], 1);
            }
        }
        for (int i : hashMap.keySet()) {
            return i;
        }
       return -1;
    }

你可能感兴趣的:(Array:Given an array of integers, every element appears three times except for one. Find that single one.)