Easy Solution by using map || Using inbuilt sorting technique 😎 || Solution in C++ and Java 💯✅
Screenshot \uD83D\uDCC3\n\n\n\n# Intuition \uD83E\uDD14\n Describe your first thoughts on how to solve this problem. \nWe need to keep the track of numbers in n
# Screenshot \uD83D\uDCC3\n![image.png](https://assets.leetcode.com/users/images/307c42a2-7513-4fe5-a667-282bb82d1a53_1721693255.3984854.png)\n\n\n# Intuition \uD83E\uDD14\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to keep the track of numbers in nums that how many times it has been occured therefore we will use `MAP` data structure for storing `key` as `nums[i]` and `value` as how many times it has been occured i.e. `freq`.\n\n \n\n# Approach \uD83E\uDE9C\n<!-- Describe your approach to solving the problem. -->\n*Steps:*\n\n1. Make a map(freq) for storing key and value i.e. nums[i] and frequency pf nums[i].\n```java []\nMap<Integer, Integer> freq = new HashMap<>();\n```\n```C++ []\nunordered_map<int, int> freq;\n```\n2. For Java, you need to convert the int to Integer type for using inbuilt sorting techniques.\n3. Assign key and value to `freq` map.\n\n```java []\nInteger[] newNums = new Integer[nums.length];\n\nfor (int i = 0; i < nums.length; i++) {\n    freq.put(nums[i], freq.getOrDefault(nums[i], 0) + 1);\n    newNums[i] = nums[i];\n}\n```\n```C++ []\nfor (int num : nums) {\n    freq[num]++;\n}\n```\n4. Now use inbuilt sorting method to sort according to the question given.\n\n```java []\nArrays.sort(newNums, (n1, n2) -> {\n    if (freq.get(n1) != freq.get(n2)) {\n                // if freq of numbers are not equal then return in increasing order based on\n                // freq.\n        return freq.get(n1) - freq.get(n2);\n    } else {\n                // otherwise sort them in decreasing order based on number in nums.\n        return n2 - n1;\n    }\n});\n```\n```C++ []\nsort(nums.begin(), nums.end(), [&](int n1, int n2) {\n    if (freq[n1] != freq[n2]) {\n                // if freq of numbers are not equal then return in increasing\n                // order based on freq.\n        return freq[n1] < freq[n2];\n    } else {\n                // otherwise sort them in decreasing order based on number in\n                // nums.\n        return n2 < n1;\n        \n});\n```\n5. Now we have aur sorted nums. For Java we will convert it into `int` and return.\n\n```java []\nfor (int i = 0; i < nums.length; i++) {\n    nums[i] = newNums[i];\n}\n\nreturn nums;\n```\n```C++ []\nreturn nums;\n```\n\n\n# Complexity \uD83D\uDC69\u200D\uD83D\uDCBB\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 \uD83D\uDE0A\uD83D\uDCBB\n```java []\nclass Solution {\n    public int[] frequencySort(int[] nums) {\n        Map<Integer, Integer> freq = new HashMap<>();\n        Integer[] newNums = new Integer[nums.length];\n\n        for (int i = 0; i < nums.length; i++) {\n            freq.put(nums[i], freq.getOrDefault(nums[i], 0) + 1);\n            newNums[i] = nums[i];\n        }\n\n        Arrays.sort(newNums, (n1, n2) -> {\n            if (freq.get(n1) != freq.get(n2)) {\n                // if freq of numbers are not equal then return in increasing order based on\n                // freq.\n                return freq.get(n1) - freq.get(n2);\n            } else {\n                // otherwise sort them in decreasing order based on number in nums.\n                return n2 - n1;\n            }\n        });\n\n        for (int i = 0; i < nums.length; i++) {\n            nums[i] = newNums[i];\n        }\n\n        return nums;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<int> frequencySort(vector<int>& nums) {\n        unordered_map<int, int> freq;\n\n        for (int num : nums) {\n            freq[num]++;\n        }\n\n        sort(nums.begin(), nums.end(), [&](int n1, int n2) {\n            if (freq[n1] != freq[n2]) {\n                // if freq of numbers are not equal then return in increasing\n                // order based on freq.\n                return freq[n1] < freq[n2];\n            } else {\n                // otherwise sort them in decreasing order based on number in\n                // nums.\n                return n2 < n1;\n            }\n        });\n\n        return nums;\n    }\n};\n```\n\nUpvote\uD83D\uDC4D and comment\u2328\uFE0F.\nPlease let me know if it helped you.\n\nTHANK YOU! \uD83D\uDE0A\n
['Array', 'Hash Table', 'Sorting', 'C++', 'Java']