Skip to content

Instantly share code, notes, and snippets.

@rnemeth90
Last active February 26, 2023 18:22
Show Gist options
  • Save rnemeth90/91abb1ba3a05196e3c4812b8297e8f34 to your computer and use it in GitHub Desktop.
Save rnemeth90/91abb1ba3a05196e3c4812b8297e8f34 to your computer and use it in GitHub Desktop.
Golang: Count of Repeating Words in String
package main
import (
"fmt"
"strings"
)
func wordFrequency(text string) map[string]int {
input := strings.Fields(text)
wc := make(map[string]int)
for _, word := range input {
_, matched := wc[word]
if matched {
wc[word] += 1
} else {
wc[word] = 1
}
}
return wc
}
func main() {
text := "The quick brown fox jumps over the lazy dog"
fmt.Println(wordFrequency(text))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment