Given an array of integers nums, sort the array in increasing order based on the frequency of each value. If multiple values share the same frequency, sort those values in decreasing order.
Return the sorted array.
Constraints:
nums.length
nums[i]
The key insight is to track how often each value appears in nums, then use that frequency information directly as a sorting key. By building a frequency map with Counter, we can sort the original array with a custom comparator: first by ascending frequency (so rarer values come first), and then by descending value (so larger values come first when frequencies tie). This transforms what looks ...
Given an array of integers nums, sort the array in increasing order based on the frequency of each value. If multiple values share the same frequency, sort those values in decreasing order.
Return the sorted array.
Constraints:
nums.length
nums[i]
The key insight is to track how often each value appears in nums, then use that frequency information directly as a sorting key. By building a frequency map with Counter, we can sort the original array with a custom comparator: first by ascending frequency (so rarer values come first), and then by descending value (so larger values come first when frequencies tie). This transforms what looks ...