K most occurring strings

Given an array arr[] of N strings and an integer K, the task is to print K strings which occurred the most number of times in the arr[]. If two or more strings have the same frequency then print the lexicographically smallest string.
Note: The value of K is always less than or equal to the number of distinct elements in the array.

Examples: 

Input: str[] = {“geeks”, “geeksforgeeks”, “geeks”, “article”}, K = 1 
Output: “geeks” 
Explanation: 
“geeks” –> 2 
“geeksforgeeks” –> 1 
“article” –> 1 
Hence, the most occurring string is “geeks”

Input: str[] = {“car”, “bus”, “car”, “bike”, “car”, “bus”, “bike”, “cycle”}, K = 2 
Output : “car”, “bus” 
Explanation: 
“car” –> 3 
“bus” –> 2 
“bike” –> 2 
“cycle” –> 1 
string “car” has highest frequency, string “bus” and “bike” both have second highest frequency, but string “bus” is lexicographically small because it has shorter length.

 

Approach:



  1. Count the frequency of each string in the array and store it in a HashMap where the string is the key and frequency as the value.
  2. Now, sort these keys according to their frequencies in ascending order, this is done to keep keys with the least frequencies at the top.
  3. The strings with equal frequencies are prioritized alphabetically, i.e., the string which is alphabetically greater has a higher priority.
  4. Delete the top N – K key-value pairs from the HashMap. By doing this the container is left with K keys with the highest frequencies, in reverse order.
  5. Print the strings stored in the HashMap.

Below is the implementation of the above approach:
 

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
  
class FrequentWords {
  
    // Function that returns list of K
    // most frequent strings
    public static ArrayList<String>
    frequentWords(ArrayList<String> arr, int K)
    {
  
        // Hash map to store the frequency
        // of each string
        HashMap<String, Integer> Freq
            = new HashMap<>();
  
        // Set the default frequency of
        // each string 0
        for (String word : arr) {
            Freq.put(word,
                     Freq.getOrDefault(word, 0)
                         + 1);
        }
  
        // Using a priority queue to store
        // the strings in accordance of the
        // frequency and alphabetical order
        // (if frequency is equal)
  
        // Lambda expression is used set the
        // priority, if frequencies are not
        // equal than the string with greater
        // frequency is returned else the
        // string which is lexically smaller
        // is returned
        PriorityQueue<String> Order
            = new PriorityQueue<>(
                (a, b)
                    -> (Freq.get(a) != Freq.get(b))
                           ? Freq.get(a) - Freq.get(b)
                           : b.compareTo(a));
  
        // Traverse the HashMap
        for (String word : Freq.keySet()) {
            Order.offer(word);
  
            // Remove top (N - K) elements
            if (Order.size() > K) {
                Order.poll();
            }
        }
  
        // Order queue has K most frequent
        // strings in a reverse order
        ArrayList<String> ans
            = new ArrayList<>();
  
        while (!Order.isEmpty()) {
            ans.add(Order.poll());
        }
  
        // Reverse the ArrayList so as
        // to get in the desired order
        Collections.reverse(ans);
  
        return ans;
    }
  
    // Driver Code
    public static void
        main(String[] args)
    {
        int N = 3, K = 2;
  
        // Given array
        ArrayList<String> arr
            = new ArrayList<String>();
        arr.add("car");
        arr.add("bus");
        arr.add("car");
  
        // Function Call
        ArrayList<String> ans
            = frequentWords(arr, K);
  
        // Print the K most occurring strings
        for (String word : ans) {
            System.out.println(word + " ");
        }
    }
}

chevron_right


Output:

car 
bus

Time Complexity: O(N*log2N)
Auxiliary Space: O(N) 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.