Word with maximum frequency
Submissions: 1312   Accuracy:


  Difficulty: Easy   Marks: 2

You are given a string that is made up of words separated by spaces. Your task is to find the word with highest frequency, i.e. it appears maximum times in the sentence. If multiple words have maximum frequency, then print the word that occurs first in the sentence.

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each testcase contains a string.

Print the word with the maximum frequency and the frequency, both separated by a space. See example for more clarity.

1<=Total Words<=100


the devil in the sky
amazon is amazon geeks for geeks is geeks for geeks
this is not right
the phenomenal one

the 2
geeks 4
this 1
excels 1
the 1

For testcase 1: The frequency of the is 2, so we print the and its frequency
For testcase 3: Every word has the frequency of 1, so we print this 1 as this occurs first in the sentence


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Ankit Sharma
Author: Soul_xhacker

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.