✅Easiest 3 Step Cpp / Java / Py / JS Solution | Two Approaches 🏆| Map+Heap
Explanation Heap\n##### Step 1: Count Frequencies\nWe use an unordered map freq_map to count the frequency of each number.\n##### Step 2:Heap (Priority Queue):\
### **Explanation Heap**\n##### **Step 1: Count Frequencies**\nWe use an unordered map freq_map to count the frequency of each number.\n##### **Step 2:Heap (Priority Queue):**\n* We use a max-heap (priority queue) to store the numbers in such a way that they are ordered by their frequency and value.\n* The custom comparator ensures that:\n* If two numbers have the same frequency, they are ordered by their value in decreasing order.\n* Otherwise, they are ordered by their frequency in increasing order\n\n##### **Step 3: Extracting from Heap:l**\nBy repeatedly removing the top element from the heap, we reconstruct the sorted array.\n\n### C++\n```\n\n//Map\nclass Solution {\npublic:\n    vector<int> frequencySort(vector<int>& nums) {\n        unordered_map<int, int> freq_map;\n        // Count the frequency of each number\n        for (int num : nums) {\n            freq_map[num]++;\n        }\n        \n        // Sort based on frequency and value\n        sort(nums.begin(), nums.end(), [&freq_map](int a, int b) {\n            // If frequencies are different, sort by frequency\n            if (freq_map[a] != freq_map[b]) {\n                return freq_map[a] < freq_map[b];\n            }\n            // If frequencies are the same, sort by value in decreasing order\n            return a > b;\n        });\n        \n        return nums;\n    }\n};\n```\n```\n//heap\nclass Solution {\npublic:\n    vector<int> frequencySort(vector<int>& nums) {\n        unordered_map<int, int> freq_map;\n        \n        // Step 1: Count the frequency of each number\n        for (int num : nums) {\n            freq_map[num]++;\n        }\n        \n        // Step 2: Use a max-heap to sort by frequency and value\n        // Define a custom comparator for the priority queue\n        auto comparator = [&freq_map](int a, int b) {\n            if (freq_map[a] == freq_map[b]) {\n                return a < b; // For the same frequency, sort by value in decreasing order\n            }\n            return freq_map[a] > freq_map[b]; // Sort by frequency in increasing order\n        };\n        \n        priority_queue<int, vector<int>, decltype(comparator)> max_heap(comparator);\n        \n        // Add all numbers to the heap\n        for (int num : nums) {\n            max_heap.push(num);\n        }\n        \n        // Step 3: Extract from the heap to get the sorted array\n        vector<int> result;\n        while (!max_heap.empty()) {\n            result.push_back(max_heap.top());\n            max_heap.pop();\n        }\n        \n        return result;\n    }\n};\n\n\n```\n### Java\n```\n\n//Unordered Map\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer, Integer> freqMap = new HashMap<>();\n        \n        // Count the frequency of each number\n        for (int num : nums) {\n            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);\n        }\n        \n        // Convert the array to Integer list for sorting\n        Integer[] numsArray = Arrays.stream(nums).boxed().toArray(Integer[]::new);\n        \n        // Sort based on frequency and value\n        Arrays.sort(numsArray, (a, b) -> {\n            if (!freqMap.get(a).equals(freqMap.get(b))) {\n                return freqMap.get(a) - freqMap.get(b); // Sort by frequency\n            } else {\n                return b - a; // Sort by value in decreasing order\n            }\n        });\n        \n        // Convert back to int array\n        return Arrays.stream(numsArray).mapToInt(Integer::intValue).toArray();\n    }\n}\n\n```\n```\n//Hashing\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer, Integer> freqMap = new HashMap<>();\n        \n        // Count the frequency of each number\n        for (int num : nums) {\n            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);\n        }\n        \n        // Use a priority queue (min-heap) to sort by frequency and value\n        PriorityQueue<Integer> heap = new PriorityQueue<>(\n            (a, b) -> freqMap.get(a).equals(freqMap.get(b)) ? b - a : freqMap.get(a) - freqMap.get(b)\n        );\n        \n        // Add all numbers to the heap\n        for (int num : nums) {\n            heap.add(num);\n        }\n        \n        // Extract from heap to get the sorted array\n        int[] result = new int[nums.length];\n        int index = 0;\n        while (!heap.isEmpty()) {\n            result[index++] = heap.poll();\n        }\n        \n        return result;\n    }\n}\n\n```\n\n### Python3\n```\n\n#Map\nclass Solution:\n    def frequencySort(self, nums: List[int]) -> List[int]:\n        freq_map = Counter(nums)\n        \n        # Sort based on frequency and value\n        nums.sort(key=lambda x: (freq_map[x], -x))\n        \n        return nums\n    \n \n ```\n ```\n#Heap\nclass Solution:\n    def frequencySort(self, nums: List[int]) -> List[int]:\n        freq_map = Counter(nums)\n        \n        # Use a heap to sort by frequency and value\n        heap = []\n        for num in nums:\n            heapq.heappush(heap, (freq_map[num], -num))\n        \n        result = []\n        while heap:\n            result.append(heapq.heappop(heap)[1] * -1)\n        \n        return result\n\n```\n### JavaScript\n```\n\n//Map\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar frequencySort = function(nums) {\n    const freqMap = new Map();\n    \n    // Count the frequency of each number\n    nums.forEach(num => {\n        freqMap.set(num, (freqMap.get(num) || 0) + 1);\n    });\n    \n    // Sort based on frequency and value\n    nums.sort((a, b) => {\n        const freqA = freqMap.get(a);\n        const freqB = freqMap.get(b);\n        if (freqA !== freqB) {\n            return freqA - freqB; // Sort by frequency\n        } else {\n            return b - a; // Sort by value in decreasing order\n        }\n    });\n    \n    return nums;\n};\n```\n```\n\n//Heap\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar frequencySort = function(nums) {\n    const freqMap = new Map();\n    \n    // Count the frequency of each number\n    nums.forEach(num => {\n        freqMap.set(num, (freqMap.get(num) || 0) + 1);\n    });\n    \n    // Create a min-heap using a priority queue\n    const heap = new MinPriorityQueue({ \n        compare: (a, b) => {\n            if (freqMap.get(a) !== freqMap.get(b)) {\n                return freqMap.get(a) - freqMap.get(b);\n            } else {\n                return b - a;\n            }\n        }\n    });\n    \n    // Add all numbers to the heap\n    nums.forEach(num => heap.enqueue(num));\n    \n    // Extract from heap to get the sorted array\n    const result = [];\n    while (!heap.isEmpty()) {\n        result.push(heap.dequeue());\n    }\n    \n    return result;\n};\n\n\n```\n#### STAY COOL STAY DISCIPLINED..\n\n![image](https://assets.leetcode.com/users/images/ad294515-01e3-4704-a668-9bcc5b0e822c_1717851160.1975598.gif)
['Array', 'Hash Table', 'C', 'Sorting', 'Heap (Priority Queue)', 'Python', 'Python3', 'JavaScript']