Word with maximum frequency
Submissions: 1210   Accuracy:

29.1%

  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.

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

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

Constraints:
1<=T<=100
1<=Total Words<=100

Example:

Input:
5
the devil in the sky
amazon is amazon geeks for geeks is geeks for geeks
this is not right
excels
the phenomenal one

Output:
the 2
geeks 4
this 1
excels 1
the 1

Explanation:
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.