Very Easy approach | Simple map + sorting | Vid explantion
Vid Explanation\nhttps://youtu.be/Ys7eYFNzgEI\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe natural intuition for this problem
# Vid Explanation\nhttps://youtu.be/Ys7eYFNzgEI\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe natural intuition for this problem is to first count the occurrences of each number in the array. This gives us a mapping between numbers and their frequencies. Then, we can sort the numbers based on their frequencies. However, for numbers with the same frequency, we need to sort them in descending order.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Count Frequencies: We use an unordered_map to store the frequency of each number. We iterate through the nums array and increment the count for each number encountered.\n2. Group by Frequency: We create a map where the key is the frequency and the value is a vector of numbers with that frequency. This groups numbers based on how many times they appear in the array.\n3. Sort within Frequency Groups: For each frequency group (key-value pair in the freqGrp map), we check if the size of the value vector (number of elements with that frequency) is greater than 1. If so, we sort the vector in descending order using sort(it.second.rbegin(), it.second.rend()). This ensures numbers with the same frequency appear in the output sorted by their value (larger values first).\n4. Build the Result: We iterate through the freqGrp map again. For each key-value pair:\nIf there are multiple elements with the same frequency (vector size > 1), iterate through the vector and append the number to the result vector ans the number of times equal to its frequency.\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> frequencySort(vector<int>& nums) {\n\n        unordered_map<int,int> freq;\n\n        for(auto &num: nums){\n\n            freq[num]++;\n            \n        }\n\n        map<int,vector<int>> freqGrp;\n\n        for(auto &it: freq){\n\n            freqGrp[it.second].push_back(it.first);\n            \n        }\n\n        vector<int> ans;\n\n        for(auto &it: freqGrp){\n\n            if(it.second.size() > 1){\n\n                sort(it.second.rbegin(), it.second.rend());\n                \n            }\n\n            for(auto &num: it.second){\n\n                for(int i=0;i<it.first;i++){\n\n                    ans.push_back(num);\n                    \n                }\n                \n            }\n            \n        }\n\n        return ans;\n        \n        \n    }\n};\n```
['C++']