Word Frequency @Dcoder

in #programminglast year

Problem: You will be given a group of words. You need to print all unique words in that group in descending order of their frequency, i.e the word repeated most number of times will come first. In case of two words having the same frequency, the word listed earlier in the group will be printed first.
Input: First line contains N, the number of words.
Next line contains N space-separated words.
Output: Print the words in descending order of their frequency
Constraints: 1 ≤ N ≤ 20
1 ≤ word[i].length ≤ 40
Sample Input: 12
can you can a can as a canner can can a can
Sample Output:
can a you as canner

input()
words = input().split()
ordered = {}
for word in words:
    if word not in ordered:
        ordered[word] = words.count( word )

print( " ".join( sorted( ordered, key=lambda k: ordered[k], reverse=True) ) )