Sort Array by Increasing Frequency

by atalaykutlay

Solution to "Sort Array by Increasing Frequency" on Leetcode

Sort Array by Increasing Frequency
1class Solution:
2    def frequencySort(self, nums: List[int]) -> List[int]:
3        freqs = collections.Counter()
4        for num in nums:
5            freqs[num*-1] += 1
6        
7        sorted_freq = sorted(freqs.items(), key = lambda x: (x[1],x[0]))
8        res = []
9        for key,value in sorted_freq:
10            for i in range(value):
11                res.append(key * -1)
12        return res

Share

Share
Video
A cool 10 sec video.
Share
Detailed
Title, description, and syntax highlighted code.
Share
Simple
Syntax highlighted code with gradient background colored presentation.

Comments

It looks like there is no comment for this snippet. Leave the first one!
You need to login to send a comment.

Similar Snippets

Count Number of Texts
Count Number of Texts

Solution to "Count Number of Texts" question on Leetcode.

Language: python15 months ago
Quicksort in Python
Quicksort in Python

An easy quicksort implementation in Python.

Language: python16 months ago
Second Minimum Node In a Binary Tree
Second Minimum Node In a Binary Tree

Solution to "Second Minimum Node In a Binary Tree" on Leetcode.

Language: python14 months ago