[LeetCode]#1636. Sort Array by Increasing Frequency

Input: nums = [1,1,2,2,2,3]
Output: [3,1,1,2,2,2]
Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.
Input: nums = [2,3,1,3,2]
Output: [1,3,3,2,2]
Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.
  1. Use Counter to provide the frequency of number.
  2. Sorted it in decreasing order and return it.
import collectionsclass Solution:
def frequencySort(self, nums):
freq = collections.Counter(nums)

return sorted(nums,key = lambda x : (freq[x], -x))

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Sage Advice About odoo enterprise vs odoo community From a Five-Year-Old

Measurement APIs: How to convert units using Unit and Measurement (Swift)

Life hack — Track your monthly expense spreadsheet through Telegram Bot

Monthly expenses spreadsheet.

Can Kubernetes Revolutionize The Enterprise World?

I am glad to share that I have successfully completed the Web development internship as a part of…

Starting Point Part 7: Markup

I Mastered Golang Basics by Solving These 15 Project Euler Problems

Image of cartoon bear.

How to make a conference app in less than 30 minutes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

[LeetCode]#2089. Find Target Indices After Sorting Array

LeetCode Patterns Adventure 12 — Palindrome Linked List

Remove nth node from end of list Leetcode Python

Finding the longest substring without repeating characters: A super-simplified explanation