Sort Frequency to Number Mapping As Per Prolem Statement | Java | C++ | [Video Solution]
Intuition, approach, and complexity discussed in video solution in detail\n\n# Code\nJava\n\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n   
# Intuition, approach, and complexity discussed in video solution in detail\n\n# Code\nJava\n```\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer, Integer> numFreqMp = new HashMap<>();\n        for(var num : nums){\n            numFreqMp.put(num, numFreqMp.getOrDefault(num, 0)+1);\n        }\n        int mappingSize = numFreqMp.size();\n        int freqNum[][] = new int[mappingSize][2];\n        int indx = 0;\n        for(var entry : numFreqMp.entrySet()){\n            freqNum[indx++] = new int[]{entry.getValue(), entry.getKey()};\n        }\n        Arrays.sort(freqNum, (a, b)->{\n            if(a[0] != b[0]){\n                return a[0] - b[0];\n            }else return b[1] - a[1];\n        });\n        List<Integer> res = new ArrayList<>();\n        for(var pair : freqNum){\n            int freq = pair[0], num = pair[1];\n            while(freq-->0){\n                res.add(num);\n            }\n        } \n        return res.stream().mapToInt(Integer::intValue).toArray();\n    }\n}\n```\nC++\n```\nclass Solution {\npublic:\n    vector<int> frequencySort(vector<int>& nums) {\n        unordered_map<int, int> numFreqMp;\n        for (int num : nums) {\n            numFreqMp[num]++;\n        }\n\n        vector<pair<int, int>> freqNum;\n        for (const auto& entry : numFreqMp) {\n            freqNum.push_back({entry.second, entry.first});\n        }\n\n        sort(freqNum.begin(), freqNum.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n            if (a.first != b.first) {\n                return a.first < b.first;\n            } else {\n                return a.second > b.second;\n            }\n        });\n\n        vector<int> res;\n        for (const auto& pair : freqNum) {\n            int freq = pair.first, num = pair.second;\n            while (freq-- > 0) {\n                res.push_back(num);\n            }\n        }\n        return res;\n    }\n};\n\n```
['Array', 'Hash Table', 'Sorting', 'C++', 'Java']