Skip to content

Commit b281021

Browse files
Time: 25 ms (40.28%), Space: 13.7 MB (72.23%) - LeetHub
1 parent 326ef72 commit b281021

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> topKFrequent(vector<int>& nums, int k) {
4+
unordered_map<int,int> m1;
5+
for(int i=0;i<nums.size();i++){
6+
m1[nums[i]]++;
7+
}
8+
vector<pair<int,int>> v1;
9+
for (auto x: m1){
10+
v1.push_back({x.second,x.first});
11+
}
12+
sort(v1.begin(), v1.end(), greater<pair<int,int>>());
13+
vector<int> v2;
14+
for(int i=0;i<k;i++){
15+
v2.push_back(v1[i].second);
16+
}
17+
return v2;
18+
}
19+
};

0 commit comments

Comments
 (0)