Java program to count the occurrence of each character in a string using Hashmap Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 36 Likes Like Report Given a string, the task is to write a program in Java which prints the number of occurrences of each character in a string. Examples: Input: str = "GeeksForGeeks" Output: r 1 s 2 e 4 F 1 G 2 k 2 o 1 Input: str = "Ajit" Output: A 1 t 1 i 1 j 1 An approach using frequency[] array has already been discussed in the previous post. In this program an approach using Hashmap in Java has been discussed. Declare a Hashmap in Java of {char, int}.Traverse in the string, check if the Hashmap already contains the traversed character or not.If it is present, then increase its count using get() and put() function in Hashmap.Once the traversal is completed, traverse in the Hashmap and print the character and its frequency. Below is the implementation of the above approach. Java // Java program to count frequencies of // characters in string using Hashmap import java.io.*; import java.util.*; class OccurrenceOfCharInString { static void characterCount(String inputString) { // Creating a HashMap containing char // as a key and occurrences as a value HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>(); // Converting given string to char array char[] strArray = inputString.toCharArray(); // checking each char of strArray for (char c : strArray) { if (charCountMap.containsKey(c)) { // If char is present in charCountMap, // incrementing it's count by 1 charCountMap.put(c, charCountMap.get(c) + 1); } else { // If char is not present in charCountMap, // putting this char to charCountMap with 1 as it's value charCountMap.put(c, 1); } } // Printing the charCountMap for (Map.Entry entry : charCountMap.entrySet()) { System.out.println(entry.getKey() + " " + entry.getValue()); } } // Driver Code public static void main(String[] args) { String str = "Ajit"; characterCount(str); } } Output: A 1 t 1 i 1 j 1 Time complexity: O(n) where n is length of given string Auxiliary Space: O(n) Create Quiz Count occurrences of character in String using HashMap in Java Comment A Ajit kumar panigrahy Follow 36 Improve A Ajit kumar panigrahy Follow 36 Improve Article Tags : Java Java Programs Java-HashMap Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like