JAVA | HashMap | Sorting | Explained
Please Upvote :D\n---\n- We create a frequency map as well as an arraylist to store the values in nums  so that we can custom sort it.\n\n- We then sort the lis
# Please Upvote :D\n---\n- We create a frequency map as well as an arraylist to store the values in nums  so that we can custom sort it.\n\n- We then sort the list in increasing order of map values (i.e. the frequency of nums elements).\n\n- If two or more values (frequencies) happens to be same, then we sort the elements itself in decreasing order. (Look at the example testcases for a better understanding).\n\n- We return the list by convertng it to an array.\n``` java []\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer, Integer> map = new HashMap<>();\n        List<Integer> ans = new ArrayList<>();\n        for (int n : nums) {                            \n            ans.add(n);\n            map.put(n, map.getOrDefault(n, 0) + 1);\n        }\n\n        Collections.sort(ans, (a, b) ->                 \n            (map.get(a) == map.get(b))? b - a : map.get(a) - map.get(b)\n        );\n\n        return ans.stream().mapToInt(i -> i).toArray(); // O(n)\n    }\n}\n\n// TC: O(n) + O(n * logn) + O(n) => O(n * logn)\n// SC: O(n)\n```
['Hash Table', 'Sorting', 'Java']