Sunday, February 19, 2023
func sortColors(nums []int) []int {
// index is a colour (0, 1, 2)
// value a is a frequency of the number in the parameter array
colourFrequencies := make([]int, 3)
for _, num := range nums {
colourFrequencies[num]++
}
sortedIndex := 0
// for each of the colours (0, 1, 2)
for i := 0; i < 3; i++ {
// iterate for frequency times of each number
for j := 0; j < colourFrequencies[i]; j++ {
// add the number to array
nums[sortedIndex] = i
sortedIndex++
}
}
return nums
}
// func sortColors(nums []int) {
// sort.Ints(nums)
// }