Maintain the counts of 0, 1 & 2.
Fill the numbers in 2nd pass.
public void sortColors(int[] nums) {
int count1 = 0, count2 = 0;
for(int i: nums){
if(i == 1) count1++;
else if(i == 2) count2++;
}
int count0 = nums.length-count1-count2;
int i = 0;
while(i < count0){
nums[i] = 0;
i++;
}
i = 0;
while(i < count1){
nums[i+count0] = 1;
i++;
}
i = 0;
while(i < count2){
nums[i+count0+count1] = 2;
i++;
}
}