Top k frequent tags

Write codeMedium15 min

Your task

A photo library shows its most-used tags first. Given the list of every tag application (repeats meaningful) and an integer k (k >= 1), return the k most frequently used distinct tags. Order the result by frequency, highest first; break frequency ties alphabetically (lexicographically ascending). If fewer than k distinct tags exist, return them all under the same ordering. For example, tags ["pop", "rock", "pop", "jazz", "rock", "pop"] with k = 2 give ["pop", "rock"], and ["t", "t", "s", "s", "r"] with k = 1 gives ["s"].

Examples

top_tags(tags: list[str], k: int) -> list[str]
Example 1
Inputtop_tags(["pop", "rock", "pop", "jazz", "rock", "pop"], 2)
Output["pop", "rock"]