Skip to content

Instantly share code, notes, and snippets.

@lemarcque
Last active February 2, 2021 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemarcque/1b8fc52f40b125fa82f37693f8494584 to your computer and use it in GitHub Desktop.
Save lemarcque/1b8fc52f40b125fa82f37693f8494584 to your computer and use it in GitHub Desktop.
Answer of the exercice "Maps" of gotour https://tour.golang.org/moretypes/23
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
splitArr := strings.Fields(s);
m := make(map[string]int)
var nbOccurence int
for _, v := range splitArr {
for _, v2 := range splitArr {
if v == v2 { nbOccurence++ }
}
m[v] = nbOccurence
nbOccurence = 0
}
return m
}
func main() {
wc.Test(WordCount)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment