Skip to content

Commit b692981

Browse files
authored
Create sort-array-by-increasing-frequency.cpp
1 parent b22b6f0 commit b692981

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
vector<int> frequencySort(vector<int>& nums) {
7+
unordered_map<int, int> count;
8+
for (const auto& x : nums) {
9+
++count[x];
10+
}
11+
sort(begin(nums), end(nums),
12+
[&count](const auto& a, const auto& b) {
13+
return count[a] == count[b] ? a > b : count[a] < count[b];
14+
});
15+
return nums;
16+
}
17+
};

0 commit comments

Comments
 (0)