Open In App

Print characters and their frequencies in order of occurrence using a LinkedHashMap in Java

Last Updated : 27 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 r1
Input: 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




// 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++) {
 
            // Using java 8 getorDefault method
            char c = str.charAt(i);
            lhm.put(c, lhm.getOrDefault(c, 0) + 1);
        }
 
        // Iterate using java 8 forEach method
        lhm.forEach(
            (k, v) -> System.out.print(k + " " + v));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        int n = str.length();
 
        printCharWithFreq(str, n);
    }
}


Output: 

g2 e4 k2 s2 f1 o1 r1

 



Similar Reads

Print characters and their frequencies in order of occurrence using Binary Tree
Given a string str containing only lowercase characters. The problem is to print the characters along with their frequency in the order of their occurrence using Binary TreeExamples: Input: str = "aaaabbnnccccz" Output: "a4b2n2c4z" Explanation: Input: str = "geeksforgeeks" Output: g2e4k2s2for Approach: Start with the first character in the string.P
14 min read
Print characters and their frequencies in order of occurrence
Given string str containing only lowercase characters. The problem is to print the characters along with their frequency in the order of their occurrence and in the given format explained in the examples below. Examples: Input : str = "geeksforgeeks" Output : g2 e4 k2 s2 f1 o1 r1 Input : str = "elephant" Output : e2 l1 p1 h1 a1 n1 t1 Source: SAP In
19 min read
Print characters having even frequencies in order of occurrence
Given a string str containing only lowercase characters. The task is to print the characters having an even frequency in the order of their occurrence.Note: Repeated elements with even frequency are printed as many times they occur in order of their occurrences.Examples: Input: str = "geeksforgeeks" Output: geeksgeeks CharacterFrequency'g'2'e'4'k'2
5 min read
Print characters having prime frequencies in order of occurrence
Given a string str containing only lowercase characters. The task is to print the characters having prime frequency in the order of their occurrence. Note that repeated elements with prime frequencies are printed as many times as they occur in order of their occurrence. Examples: Input: str = "geeksforgeeks" Output: gksgks CharacterFrequency'g'2'e'
11 min read
Print characters having odd frequencies in order of occurrence
Given a string str containing only lowercase characters. The task is to print the characters having an odd frequency in the order of their occurrence.Note: Repeated elements with odd frequency are printed as many times they occur in order of their occurrences.Examples: Input: str = "geeksforgeeks" Output: for CharacterFrequency'g'2'e'4'k'2's'2'f'1'
7 min read
Merge transactions in bank sheets in the order of their occurrence such that their sum remains positive
Given an array arr[][] consisting of N lists representing N transactions, the task is to merge the given lists of transactions in the order of their occurrences, such that at any point of time, the sum of already performed transactions is non-negative. If found to negative, then print "-1". Otherwise, print the merged list of transactions. Examples
7 min read
Print numbers in descending order along with their frequencies
Given an array arr, the task is to print the elements of the array in descending order along with their frequencies.Examples: Input: arr[] = {1, 3, 3, 3, 4, 4, 5} Output: 5 occurs 1 times 4 occurs 2 times 3 occurs 3 times 1 occurs 1 timesInput: arr[] = {1, 1, 1, 2, 3, 4, 9, 9, 10} Output: 10 occurs 1 times 9 occurs 2 times 4 occurs 1 times 3 occurs
11 min read
Print 2-D co-ordinate points in ascending order followed by their frequencies
Given two arrays x[] and y[] where x[i] represents the x coordinate and y[i] represent the corresponding y coordinate of a 2-D point, the task is to print the coordinate points in ascending order followed by their frequencies.Examples: Input: x[] = {1, 2, 1, 1, 1}, y[] = {1, 1, 3, 1, 3} Output: 1 1 2 1 3 2 2 1 1Input: x[] = {-1, 2, 1, -1, 2}, y[] =
5 min read
Print all unique digits present in concatenation of all array elements in the order of their occurrence
Given an array arr[] consisting of N integers, the task is to print all unique digits of the number formed by concatenating all array elements in the order of their occurrence after excluding leading zeroes. Examples: Input: arr[] = {122, 474, 612, 932}Output: 7 6 9 3Explanation:The number formed by concatenating array elements is “122474612932”.Un
10 min read
Print the Vowels in the Order of their occurrence in the given Matrix
Given a character matrix arr[][] of dimensions 3 * N, consisting of three characters {#, *, . }, the task is to find the vowels(A, E, I, O, U) represented by '*' from the given string. Note: Vowel A is denoted in a 3x3 block, as shown in the examples below. Explanation: Input: N = 18 * . * # * * * # * * * # * * * . * . * . * # * . * # . * . # * * *
8 min read
Article Tags :