C++ simple solution


  • 0
    S

    class Solution {
    public:
    vector<int> topKFrequent(vector<int>& n, int k) {
    map<int,int > mp;
    for(int i=0;i<n.size();i++)
    {
    mp[n[i]]++;
    }
    vector<pair <int ,int > > v;

        for(auto i : mp)
        {
            v.push_back(make_pair(i.second,i.first));
            cout<<i.second<<endl;
        }
        sort(v.begin(),v.end());
        
        vector <int> res;
        int i=v.size()-1;
        while(k--){
            cout<<v[i].second<<endl;
            res.push_back(v[i--].second);
        }
        return res;    
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.