K most frequent values
Your task
Given a list of integers and an integer k, return the k most frequent values, ordered from most frequent to least frequent. When two values occur the same number of times, the smaller value comes first. You may assume k is at least 1 and at most the number of distinct values. Example: nums = [4, 1, 4, 2, 2, 4], k = 2 gives [4, 2], 4 appears three times and 2 appears twice, so 1 (seen once) misses the cut.
Examples
top_frequent(nums: list[int], k: int) -> list[int]Example 1
Input
top_frequent([4, 1, 4, 2, 2, 4], 2)Output
[4, 2]