Fetching the code, complexity notes and tags. Just a moment.
This snippet demonstrates how to count the frequency of each character in a string using Java's HashMap. It iterates through every character, updates its count, and finally prints the frequency of each character. This technique is widely used in string manipulation, anagram checking, and text-processing problems during coding interviews.
Category
hashmap
Complexity
Time: O(n) | Space: O(n)
Language
Java
Status
Tags
Related Concepts
Published
Last Updated
Production-ready Java implementation for quick revision, interview preparation, and real-world development.
Solution.java
1import java.util.HashMap;2import java.util.Map;3 4public class CharacterFrequency {5 6 public static void countFrequency(String text) {7 8 HashMap<Character, Integer> frequencyMap = new HashMap<>();9 10 for (char ch : text.toCharArray()) {11 frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);12 }13 14 System.out.println("Character Frequencies:");15 16 for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {17 System.out.println(entry.getKey() + " : " + entry.getValue());18 }19 }20 21 public static void main(String[] args) {22 23 String text = "programming";24 25 countFrequency(text);26 }27}Keywords
Explore the important concepts and keywords associated with this Java snippet.
💡 These tags help you quickly identify the concepts covered in this snippet and make it easier to discover similar Java solutions throughout the library.