Java | TreeMap | ArrayList | Sorting | Easy Solution
\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer,Integer> map1 = new TreeMap<>();\n        for(int i: nums)\n            ma
```\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer,Integer> map1 = new TreeMap<>();\n        for(int i: nums)\n            map1.put(i,map1.getOrDefault(i,0)+1);\n        Map<Integer,List<Integer>> map2 = new TreeMap<>();\n        for(Map.Entry m: map1.entrySet()){\n            int num = (int)m.getKey();\n            int fre = (int)m.getValue();\n            if(map2.containsKey(fre)){\n                List<Integer> l = new ArrayList<>(map2.get(fre));\n                l.add(num);\n                map2.replace(fre,l);\n            }\n            else{\n                List<Integer> l = new ArrayList<>();\n                l.add(num);\n                map2.put(fre,l);\n            }\n        }\n        int arr[] = new int[nums.length];\n        int index=0;\n        for(Map.Entry m: map2.entrySet()){\n            int fre = (int)m.getKey();\n            List<Integer> l = new ArrayList<>(map2.get(fre));\n            Collections.sort(l);\n            for(int i=l.size()-1;i>=0;i--){\n                for(int k=0;k<fre;k++){\n                    arr[index]=l.get(i);\n                    index++;\n                }\n            }\n        }\n        return arr;\n    }\n}\n```\nKindly upvote if you like the code.
['Array', 'Tree', 'Sorting', 'Java']