Search code examples
javanullpointerexceptionhashmapiteratorinteger

Getting java.lang.NullPointerException when removing key from hashmap in Java


https://leetcode.com/problems/top-k-frequent-elements/

the problem occurs at the line:

hm.remove(currentGreatestValue);

class Solution {
    public int[] topKFrequent(int[] nums, int k) {

        HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
        for (int i=0; i<nums.length; i++){
            if (!hm.containsKey(nums[i])){
                hm.put(nums[i], 1);
            } else {
                int occurances = hm.get(nums[i]);
                occurances = occurances + 1;
                hm.put(nums[i], occurances);
            }
        }
   
        int mostOccuredValues[] = new int[k]; 

        Iterator hmIterator = hm.entrySet().iterator();

        int currentGreatestValue=0;
        int currentValue;

        int nextGreatestIndex=0;


        for (int i=0; i<k; i++){
            while (hmIterator.hasNext()) {
                Map.Entry mapElement = (Map.Entry)hmIterator.next();
                currentValue = (int)mapElement.getKey();
                if (currentValue>currentGreatestValue){
                    currentGreatestValue = currentValue;
                }
            }
            mostOccuredValues[nextGreatestIndex] = hm.get(currentGreatestValue);
            nextGreatestIndex = nextGreatestIndex + 1;
 
            hm.remove(currentGreatestValue);
        }

        return mostOccuredValues;
    }
}

want to remove key from hashmap to get the next greatest element while iterating


Solution

  • class Solution { public int[] topKFrequent(int[] nums, int k) {

        HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
        for (int i=0; i<nums.length; i++){
            if (!hm.containsKey(nums[i])){
                hm.put(nums[i], 1);
            } else {
                int occurances = hm.get(nums[i]);
                occurances = occurances + 1;
                hm.put(nums[i], occurances);
            }
        }
    
        int mostOccuredValues[] = new int[k]; 
    
        int currentValue;
        int currentKey;
    
        int nextGreatestIndex=0;
    
    
        for (int i=0; i<k; i++){
            int currentGreatestValue=0;
            int currentGreatestKey=0;
            
            Iterator hmIterator = hm.entrySet().iterator();
            while (hmIterator.hasNext()) {
                Map.Entry mapElement = (Map.Entry)hmIterator.next();
                currentValue = (int)mapElement.getValue();
                currentKey = (int)mapElement.getKey();
                
                if (currentValue>currentGreatestValue){
                    currentGreatestValue = currentValue;
                    currentGreatestKey = currentKey;
                }
            }
    
            mostOccuredValues[nextGreatestIndex] = currentGreatestKey;
            nextGreatestIndex = nextGreatestIndex + 1;
    
            hm.remove(currentGreatestKey);
        }
    
        return mostOccuredValues;
    }
    

    }