> For the complete documentation index, see [llms.txt](https://chiragjain.gitbook.io/neetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chiragjain.gitbook.io/neetcode/347-top-k-frequent-elements/code.md).

# CODE

```python
class Solution:
    def topKFrequent(self, nums: List[int], k: int) -> List[int]:
        # Get count of each number
        count = {}
        for n in nums:
            count[n] = 1 + count.get(n, 0)
            
        # Create a dict freq -> all the numbers with that freq
        freq = {}
        for n, f in count.items():
            if f not in freq:
                freq[f] = []
            freq[f].append(n)

        res = []
        for f in range(len(nums), 0, -1):
            # get all the numbers for the freq starting with highest
            if f in freq:
                for n in freq[f]:
                    res.append(n)
                    if len(res) == k:
                        return res
```
