Last active
February 2, 2021 22:28
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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