imashutosh51

Sorting Elements of an Array by Frequency

Jul 31st, 2022 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. class Solution:
  2.     def frequencySort(self, nums: List[int]) -> List[int]:
  3.         dict={}
  4.         for i in nums:
  5.             dict[i]=dict.get(i,0)+1
  6.         arr=[[v,k] for k,v in dict.items()]
  7.         arr.sort(key=lambda x: (x[0],-x[1]))
  8.         ans=[]
  9.         for v,k in arr:
  10.             for j in range(v):
  11.                 ans.append(k)
  12.         return ans
  13.        
Advertisement
Add Comment
Please, Sign In to add comment