In this post we’ll see a Java program to count the frequency of each character in a string. Here two ways of counting the number of times each character appears in a String are given.
- Using HashMap where character is the key and count is the value. For every character you need to verify if the key already exists in the HashMap. If it does, increase the count otherwise add it to the map as a new key with count 1. See example.
- Another way is using char array where you will have to take the char at the first index of the array and iterate the whole array to check if that char is found again, if yes increment the count. At the end replace() method of the String class is used to remove all the occurrences of that character. See example.
Java program Using HashMap to count the frequency of each character
import java.util.HashMap; import java.util.Map; import java.util.Set; public class CountChars { public static void main(String[] args) { CountChars.countCharactersMap("This is a test line"); } public static void countCharactersMap(String str){ Map<Character, Integer> charMap = new HashMap<Character, Integer>(); for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); // For space or tab skip the process if(((c == ' ') || (c == '\t'))){ continue; } // if character is already there in the map // increment the count if(charMap.containsKey(c)){ charMap.put(c, charMap.get(c) + 1); }else{ // If new character put that character in the map // with the value as 1 charMap.put(c, 1); } } // Displaying the map values Set<Map.Entry<Character, Integer>> numSet = charMap.entrySet(); for(Map.Entry<Character, Integer> m : numSet){ System.out.println(m.getKey() + " - " + m.getValue()); } } }
Output
a - 1 s - 3 T - 1 t - 2 e - 2 h - 1 i - 3 l - 1 n - 1
Using char array and String replace() method
Here is a Java program to count the frequency of each character in a String using char array and String replace() method .
public class CountChars { public static void main(String[] args) { CountChars.countCharacters("This is a test line"); } public static void countCharacters(String str){ char[] strArr; do{ strArr = str.toCharArray(); char ch = strArr[0]; int count = 1; for(int i = 1; i < strArr.length; i++){ if(ch == strArr[i]){ count++; } } // don't display for space or tab if(((ch != ' ') && (ch != '\t'))){ System.out.println(ch + " - " + count); } // replace all occurrence of the character // which is already counted str = str.replace(""+ch, ""); // If string is of length 0 then come // out of the loop if(str.length() == 0) { break; } }while(strArr.length > 1); } }
Output
T - 1 h - 1 i - 3 s - 3 a - 1 t - 2 e - 2 l - 1 n - 1
Related Posts
- Java Program to Count Number of Words in a String
- Java Program to Find Duplicate Characters in a String With Repetition Count
- How to Split a String in Java
- Find Length of String Without Using length() Method in Java
- Java Program to Find First Non-Repeated Character in The Given String
- Check Given Number Palindrome or Not in Java
- Remove Element From an Array in Java
- Decompress And Untar Multiple Gzipped files in Java
That’s all for the topic Java Program to Count The Frequency of Each Character in a String. If something is missing or you have something to share about the topic please write a comment.
You may also like
Hi, for the second solution, I noticed that it will produce error if there are two same characters next to each other. For example: This is a linee. How do I solve it?
Thanks for pointing it out, best to check if string length becomes 0 at any time then break. I have changed the code too.
if(str.length() == 0) {
break;
}