K most frequent elements

Write codeMedium25 minMeta and Amazon style

Your task

Given an integer array a and an integer k, return the k most frequent elements, returned as a list sorted in ascending order by value. It is guaranteed the answer is unique (no ties at the frequency cutoff). 1 <= k <= number of distinct values.

Examples

top_k_frequent(a: list[int], k: int) -> list[int]
Example 1
Inputtop_k_frequent([1, 1, 1, 2, 2, 3], 2)
Output[1, 2]