August 26, 2022 |16.4K Views

    Count occurrences of character in String using HashMap in Java

    Description
    Discussion

    In this video, we will be seeing how to count occurrences of character in String using HashMap in Java.

    Following would be the steps that we will be using:

    1) Declare a Hashmap in Java of {char, int}.
    2) Traverse in the string, check if the Hashmap already contains the traversed character or not.
    3) If it is present, then increase its count using get() & put() function in Hashmap
    4) Once the traversal is completed, traverse in the Hashmap and print the character and its frequency.

    Java program to count the occurrence of each character in a string using Hashmap
    https://www.geeksforgeeks.org/java-program-to-count-the-occurrence-of-each-character-in-a-string-using-hashmap/