Popular hashtags
Write code · Medium · §6.1 Putting things in order
tags holds the hashtags used in posts. If a hashtag was used several times, it appears in tags that many times.
Complete the function solution, which returns a list of the k most used hashtags, from most used to least used. Hashtags used the same number of times are listed in ascending order, the way sorted() sorts strings.
If there are fewer than k different hashtags, include all of them, following the same rules.
Function
solution(tags, k)
Constraints
- The length of
tagsis from 1 to 100,000. - Each hashtag is a string of lowercase English letters and digits, with a length from 1 to 20.
kis an integer from 1 to 100,000.
Examples
| tags | k | result |
|---|---|---|
['ai', 'cat', 'ai', 'food', 'cat', 'ai'] | 2 | ['ai', 'cat'] |
['dog', 'dog', 'music', 'travel', 'travel', 'travel'] | 3 | ['travel', 'dog', 'music'] |
Starter code
def solution(tags, k):
answer = []
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.