Sort Colors

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 nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library's sort function.
LeetCode Problem - 75
class Solution {
public void sortColors(int[] nums) {
// Create a HashMap to count occurrences of each color (0, 1, or 2)
Map<Integer, Integer> countMap = new HashMap<>();
// Count occurrences of each color in nums array
for (int num : nums) {
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
}
int idx = 0; // Index to update nums array
// Iterate through the HashMap and reconstruct nums array in sorted order
for (int color : countMap.keySet()) {
int count = countMap.get(color);
// Place each color in nums array according to its count
for (int i = 0; i < count; i++) {
nums[idx++] = color;
}
}
}
}




