Hashmap implementation to count the occurrences of each character

You’re leaving char ch set as the same character through each execution of the loop.

It should be:

ch = char_array[i]; 
if(charCounter.containsKey(ch)){
     charCounter.put(ch, charCounter.get(ch)+1);
}
else
{
    charCounter.put(ch, 1);
}

Inside the for loop.

Leave a Comment