#K92237. Top Three Words
Top Three Words
Top Three Words
Given a sequence of words, your task is to identify the three words with the highest frequency. In case of a tie (i.e. when two or more words have the same frequency), the word that appears earlier in the input should come first.
Formally, for each word (w), let (f(w)) be its frequency (i.e. the number of occurrences). You need to output the top three words sorted primarily by (-f(w)) and secondarily by their first occurrence position in the input.
If there are less than three distinct words, output all of them in the described order. If the input list is empty, print nothing.
inputFormat
The input is read from standard input (stdin) and has the following format:
The first line contains an integer (n) ((n \geq 0)) denoting the number of words. Each of the following (n) lines contains a single word.
outputFormat
Output to standard output (stdout) a single line containing the top three words (or all words if there are fewer than three) separated by a single space. If there are no words, output an empty line.## sample
5
apple
banana
kiwi
orange
grape
apple banana kiwi
</p>