Java easy to understand solution using HashMap


  • 1
    F
        HashMap<Integer, Integer> Map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (!Map.containsKey(nums[i])) {
                Map.put(nums[i], 1);
            } else {
                if (Map.get(nums[i]) == 2) {
                    Map.remove(nums[i]);
                } else {
                    Map.put(nums[i], Map.get(nums[i]) + 1);
                }
            }
        }
        Iterator iter = Map.keySet().iterator();
        int result = (int)iter.next();
        return result;

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.