🌟🔥 Easy Solution and Detailed Explanation || ✅ C++ || ✅ Java || ✅ Python 🔥🌟
Approach\n### Step 1: Counting Frequencies \uD83D\uDCCA\nWe start by creating a HashMap to count the frequency of each element in the array.\n\njava\nHashMap<In
# Approach\n### Step 1: Counting Frequencies \uD83D\uDCCA\nWe start by creating a `HashMap` to count the frequency of each element in the array.\n\n```java\nHashMap<Integer, Integer> map = new HashMap<>();\nfor (int i = 0; i < nums.length; i++) {\n    map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);\n}\n```\n- **Explanation:** This loop iterates over the array `nums`, and for each element, it increments its count in the `map`. If the element is not present in the map, `getOrDefault` initializes it with `0`.\n\n### Step 2: Convert to Integer Array \uD83D\uDD04\nNext, we convert the `int` array to an `Integer` array to use `Arrays.sort` with a custom comparator.\n\n```java\nInteger[] arrobj = new Integer[nums.length];\nfor (int i = 0; i < nums.length; i++) {\n    arrobj[i] = nums[i];\n}\n```\n- **Explanation:** We create a new `Integer` array `arrobj` and copy each element from the `nums` array into `arrobj`.\n\n### Step 3: Custom Sorting \uD83D\uDCDA\nWe sort the `arrobj` array based on the custom comparator:\n- By frequency in ascending order.\n- By value in descending order if frequencies are the same.\n\n```java\nArrays.sort(arrobj, (a, b) -> {\n    if (map.get(a).equals(map.get(b))) {\n        return Integer.compare(b, a); // Sort by value in descending order if frequencies are the same\n    }\n    return Integer.compare(map.get(a), map.get(b)); // Sort by frequency in ascending order\n});\n```\n- **Explanation:** We define a custom comparator:\n  - If two elements have the same frequency (`map.get(a).equals(map.get(b))`), we sort them by their value in descending order (`Integer.compare(b, a)`).\n  - Otherwise, we sort them by their frequency in ascending order (`Integer.compare(map.get(a), map.get(b))`).\n\n### Step 4: Convert Back to int Array \uD83D\uDD04\nFinally, we convert the sorted `Integer` array back to the `int` array to return the result.\n\n```java\nfor (int i = 0; i < nums.length; i++) {\n    nums[i] = arrobj[i];\n}\n\nreturn nums;\n```\n- **Explanation:** We copy each element from the `arrobj` array back into the `nums` array.\n\n### Full Code Implementation \uD83D\uDCDD\n```C++ []\n#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n    std::vector<int> frequencySort(std::vector<int>& nums) {\n        // \uD83D\uDCCA Create an unordered_map to store the frequency of each element\n        std::unordered_map<int, int> map;\n        for (int num : nums) {\n            map[num]++;\n        }\n\n        // \uD83D\uDCDA Sort the array based on the custom comparator\n        std::sort(nums.begin(), nums.end(), [&map](int a, int b) {\n            if (map[a] == map[b]) {\n                return a > b; // Sort by value in descending order if frequencies are the same\n            }\n            return map[a] < map[b]; // Sort by frequency in ascending order\n        });\n\n        return nums;\n    }\n};\n\n```\n```java []\nimport java.util.*;\n\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        // \uD83D\uDCCA Create a hashmap to store the frequency of each element\n        HashMap<Integer, Integer> map = new HashMap<>();\n        for (int i = 0; i < nums.length; i++) {\n            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);\n        }\n\n        // \uD83D\uDD04 Convert the array to an Integer array to use Arrays.sort with a custom comparator\n        Integer[] arrobj = new Integer[nums.length];\n        for (int i = 0; i < nums.length; i++) {\n            arrobj[i] = nums[i];\n        }\n\n        // \uD83D\uDCDA Sort the array based on the custom comparator\n        Arrays.sort(arrobj, (a, b) -> {\n            if (map.get(a).equals(map.get(b))) {\n                return Integer.compare(b, a); // Sort by value in descending order if frequencies are the same\n            }\n            return Integer.compare(map.get(a), map.get(b)); // Sort by frequency in ascending order\n        });\n\n        // \uD83D\uDD04 Convert the sorted array back to int array\n        for (int i = 0; i < nums.length; i++) {\n            nums[i] = arrobj[i];\n        }\n\n        return nums;\n    }\n}\n```\n```python []\nfrom collections import Counter\n\nclass Solution:\n    def frequencySort(self, nums):\n        # \uD83D\uDCCA Create a Counter to store the frequency of each element\n        count = Counter(nums)\n\n        # \uD83D\uDCDA Sort the array based on the custom comparator\n        nums.sort(key=lambda x: (count[x], -x))\n\n        return nums\n\n```\n![UP.png](https://assets.leetcode.com/users/images/1ee5263a-0285-4918-bfda-5df001bd1796_1721705785.7250383.png)\n
['Array', 'Hash Table', 'Sorting', 'Python', 'C++', 'Java']