Monday, July 22, 2024
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.
import (
"sort"
)
func frequencySort(nums []int) []int {
// key = number
// value = frequency of this number in array
frequencyMap := make(map[int]int)
for _, num := range nums {
frequencyMap[num]++
}
sort.Slice(nums, func(i, j int) bool {
// comparing frequency of numbers from recorded frequency map
if frequencyMap[nums[i]] == frequencyMap[nums[j]] {
// increasing order
return nums[i] > nums[j]
}
// decreasing order
return frequencyMap[nums[i]] < frequencyMap[nums[j]]
})
return nums
}