[LeetCode] Sort Array by Increasing Frequency

1636. Sort Array by Increasing Frequency

Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.

Return the sorted array.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
vector<int> frequencySort(vector<int>& nums) {
unordered_map<int,int> freq;
for(auto& n : nums) freq[n]++;
sort(begin(nums), end(nums), [&](int i, int j) {
if(freq[i] != freq[j]) return freq[i] < freq[j];
return i > j;
});
return nums;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/07/23/PS/LeetCode/sort-array-by-increasing-frequency/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.