Dcoder icon

Word Frequency @Dcoder

By Lochard | As a newbie programmer | 4 Aug 2023


Title: Word Frequency

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) ) )

How do you rate this article?

1


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.