#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    vector<int> topKFrequent(vector<int> &nums, int k)
    {

        unordered_map<int, int> nums_count;
        for (auto x : nums)
        {
            nums_count[x]++;
        }

        multimap<int, int, greater<int>> big_m;
        for (auto x : nums_count)
        {
            big_m.insert(make_pair(x.second, x.first));
        }

        vector<int> res;
        for (auto it = big_m.begin(); it != big_m.end() && k; it++, k--)
        {
            res.push_back(it->second);
        }
        return res;
    }
};
