package main import "fmt" func main() { str := "banana" freq := make(map[rune]int) for _, ch := range str { freq[ch]++ } fmt.Println(freq) }
The code counts each character's occurrences in the string "banana". The letter 'a' appears 3 times, 'b' once, and 'n' twice. The map keys are rune values representing characters.
The frequency counter pattern uses a hash map to store counts of each element, allowing quick lookup and counting.
package main import "fmt" func main() { nums := []int{2, 3, 2, 4, 3, 2} freq := make(map[int]int) for _, num := range nums { freq[num]++ } fmt.Println(freq) }
The slice contains 2 three times, 3 two times, and 4 once. The map stores these counts.
package main import "fmt" func main() { str := "hello" freq := make(map[int]int) for _, ch := range str { freq[ch]++ } fmt.Println(freq) }
In Go, rune is an alias for int32. The map is map[int]int, where int is typically 64-bit on 64-bit systems. There is no type mismatch: rune can be used as key for map[int]int because rune is an alias for int32 and can be used as int key. This code compiles and runs correctly, outputting the frequency map.
Option A correctly increments for s1 and decrements for s2 in paired loops (using indices), then checks all counts are zero. Option A fails to compile (maps not comparable). Option A incorrectly checks counts == 2. Option A checks for negatives but does not explicitly verify remaining counts are zero (though it works due to length equality, A is more explicit).
