Leetcode Note: Go - Sort Array by Increasing Frequency

Sort Array by Increasing Frequency - LeetCode
https://leetcode.com/problems/sort-array-by-increasing-frequency/

  • Go 言語で取り組んだメモ

回答

Go Solution - Sort Array by Increasing Frequency - LeetCode
https://leetcode.com/problems/sort-array-by-increasing-frequency/solutions/933568/go-solution/

func frequencySort(nums []int) []int {
    seen := map[int]int{}

	for _, v := range nums {
		seen[v]++
	}

    sort.Slice(nums, func(i, j int) bool{
        if seen[nums[i]] == seen[nums[j]] {
            return nums[j] < nums[i]
        }
        
        return seen[nums[i]] < seen[nums[j]]
    })

	return nums
}