Sort Array by Increasing Frequency

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order.
Return the sorted array.
LeetCode Problem - 1636
class Solution {
public int[] frequencySort(int[] nums) {
// Convert the input array to an Integer array to facilitate custom sorting
Integer[] newNums = new Integer[nums.length];
// Create a map to store the frequency of each number in the array
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
newNums[i] = nums[i];
}
// Sort the array based on the custom comparator
Arrays.sort(newNums, (n, m) -> {
// Compare the frequency of the two numbers
if (map.get(n) != map.get(m)) {
return map.get(n) - map.get(m); // Ascending order of frequency
} else {
return m - n; // Descending order of values if frequencies are equal
}
});
// Convert the sorted Integer array back to an int array
for (int i = 0; i < nums.length; i++) {
nums[i] = newNums[i];
}
// Return the sorted array
return nums;
}
}




