Java program to count the occurrence of each character in a string using Hashmap Last Updated : 01 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article 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) Count occurrences of character in String using HashMap in Java Comment More infoAdvertise with us Next Article Java program to count the characters in each word in a given sentence A Ajit kumar panigrahy Follow Improve Article Tags : Java Java Programs Java-HashMap Practice Tags : Java Similar Reads Java Program to Find the Occurrence of Words in a String using HashMap HashMap<Key, Value> provides the basic implementation of the Map interface of Java and import java.util.HashMap package or its superclass. HashMap stores the data in (Key, Value) pairs, and accessed by an index of another type (e.g. an Integer). One object is used as a key to another object. I 3 min read Java Program to Count the Occurrences of Each Character In Java, counting the occurrences of each character in a string is a fundamental operation that can be done in different ways. This process involves identifying the frequency of each character in the input string and displaying it in a readable format.Example: Input/output to count the occurrences o 5 min read Count Occurrences of a Given Character using Regex in Java Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in 2 min read Java Program to Check Strings Anagram Using HashMap Java Program to check whether two given strings are anagrams of each other or not using HashMap. What is an Anagram?A string is an anagram of another string if it contains the same characters in the same or in different order. Example of Anagram in JavaInput: s1 = "abcd" s2 = "cadb"Output: Two Strin 3 min read Java program to count the characters in each word in a given sentence Write a Java program to count the characters in each word in a given sentence? Examples: Input : geeks for geeksOutput :geeks->5for->3geeks->5 Recommended: Please solve it on PRACTICE first, before moving on to the solution. Approach:Here we have to find out number of words in a sentence an 3 min read Java program to print all duplicate characters in a string Given a string, the task is to write Java program to print all the duplicate characters with their frequency Example: Input: str = "geeksforgeeks" Output: s : 2 e : 4 g : 2 k : 2 Input: str = "java" Output: a : 2 Approach: The idea is to do hashing using HashMap. Create a hashMap of type {char, int} 2 min read Like