Chapter 5 - Arrays
Exercise 2: Counting Occurrences
We’ve set up a small array that contains the numbers 0, 1, and 2, each repeated a random number of times. Update the program to count how many times each number occurs.
Hint: You can set up a second array to keep track of the number of occurrences. A three-element array will (conveniently) have the indexes 0, 1, and 2…
Solution
package main
import "fmt"
func main() {
// We'll count the number of times each number occurs
// within this array.
numbers := [9]int{1, 0, 2, 0, 1, 0, 0, 1, 2}
// "occurrences" will have the indexes 0, 1, and 2.
var occurrences [3]int
// Process each element in "numbers". We ignore the
// index because we don't need it.
for _, number := range numbers {
// The zero value for elements in "occurrences"
// is 0. So we can safely increment it even if
// we've never assigned to it.
occurrences[number]++
}
// Finally, print out the number of times each number
// occurred.
for number, count := range occurrences {
fmt.Println(number, "occurred", count, "times")
}
}
Output:
0 occurred 4 times
1 occurred 3 times
2 occurred 2 times
Extra credit:
package main
import "fmt"
func main() {
numbers := [100]int{4, 2, 6, 7, 8, 0, 1, 8, 7, 8,
1, 5, 3, 2, 2, 1, 9, 6, 1, 0, 0, 0, 8, 4, 6,
2, 2, 4, 7, 9, 6, 5, 9, 0, 5, 1, 1, 5, 4, 7,
7, 9, 7, 8, 6, 3, 3, 3, 4, 8, 0, 4, 1, 1, 7,
9, 8, 8, 1, 2, 3, 6, 4, 9, 2, 5, 8, 6, 7, 7,
5, 4, 2, 9, 4, 4, 2, 2, 5, 5, 0, 0, 0, 9, 1,
9, 5, 8, 0, 1, 1, 0, 5, 3, 8, 6, 3, 4, 4, 9}
var occurrences [10]int
for _, number := range numbers {
occurrences[number]++
}
for number, count := range occurrences {
fmt.Println(number, "occurred", count, "times")
}
}
Output:
0 occurred 11 times
1 occurred 12 times
2 occurred 10 times
3 occurred 7 times
4 occurred 12 times
5 occurred 10 times
6 occurred 8 times
7 occurred 9 times
8 occurred 11 times
9 occurred 10 times