美文网首页
Array:Given an array of integers

Array:Given an array of integers

作者: 敲一手烂代码 | 来源:发表于2016-06-12 17:07 被阅读16次
public int singleNumber1(int[] nums) {
        HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        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

      本文链接:https://www.haomeiwen.com/subject/iagedttx.html