Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java
Given a string str containing only lowercase characters. The task is to print the characters along with their frequencies in the order of their occurrence in the given string.
Examples:
Input: str = “geeksforgeeks”
Output: g2 e4 k2 s2 f1 o1 r1Input: str = “helloworld”
Output: h1 e1 l3 o2 w1 r1 d1
Approach: Traverse the given string character by character and store the frequencies of all the strings in a LinkedHashMap which maintains the order of the elements in which they are stored. Now, iterate over the elements of the LinkedhashMap and print the contents.
Below is the implementation of the above approach:
// Java implementation of the approach import java.util.LinkedHashMap; public class GFG { // Function to print the characters and their // frequencies in the order of their occurrence static void printCharWithFreq(String str, int n) { // LinkedHashMap preserves the order in // which the input is supplied LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<Character, Integer>(); // For every character of the input string for (int i = 0; i < n; i++) { // Get the frequency of the current character Integer j = lhm.get(str.charAt(i)); // If the chracter is occurring // for the first time if (j == null) lhm.put(str.charAt(i), 1); // Increment the frequency of the // current character by 1 else lhm.put(str.charAt(i), j + 1); } // Iterate over the LinkedHashMap // and print the result for (Character c : lhm.keySet()) System.out.print("" + c + lhm.get(c) + " "); } // Driver code public static void main(String[] args) { String str = "geeksforgeeks"; int n = str.length(); printCharWithFreq(str, n); } } |
g2 e4 k2 s2 f1 o1 r1
Recommended Posts:
- Print characters having odd frequencies in order of occurrence
- Print characters having even frequencies in order of occurrence
- Print characters and their frequencies in order of occurrence
- Print 2-D co-ordinate points in ascending order followed by their frequencies
- Print the last occurrence of elements in array in relative order
- Print common characters of two Strings in alphabetical order
- Print all distinct characters of a string in order (3 Methods)
- Python code to print common characters of two Strings in alphabetical order
- Queries for frequencies of characters in substrings
- XOR of Prime Frequencies of Characters in a String
- Sum and Product of Prime Frequencies of Characters in a String
- Check whether the frequencies of all the characters in a string are prime or not
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- LinkedHashMap in Java
- LinkedHashMap get() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



