Given a huge set of words with duplicates present, find the maximum occurring word in it. If two words have the same count, return any one of them.

For example,

Input:
 
keys = [code, coder, coding, codable, codec, codecs, coded, codeless, codec, codecs, codependence, codex, codify, codependents, codes, code, coder, codesign, codec, codeveloper, codrive, codec, codecs, codiscovered]
 
Output:
 
The maximum occurring word is codec.
Its count is 4

Practice this problem

The idea is to use Trie (Prefix Tree) to solve this problem. We start by inserting each key into the Trie and store its count so far (along with the key itself) in the leaf nodes. After all nodes are inserted into the Trie, perform its preorder traversal (DFS), and find the maximum frequency word by comparing the count present at leaf nodes. Note that we can also use a map to solve this problem.

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

Word : codec
Count: 4

Java


Download  Run Code

Output:

Word : codec
Count: 4

Python


Download  Run Code

Output:

Word : codec
Count: 4

The time complexity of the above solution is O(N.M), where N is the total number of given words and M is the maximum word length. The auxiliary space required by the program is O(N × M).

 
Exercise:

1. Extend this solution to print all maximum occurring words (having the same count).

2. Extend this solution to print the first k maximum occurring word.