Created
November 16, 2022 09:23
-
-
Save Aadithya-V/bd63761d1cb3265f764c15a73e70958f to your computer and use it in GitHub Desktop.
Go Tour Map Exercised
This file contains 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 { | |
tokens := strings.Fields(s) | |
m := make(map[string]int) | |
for _, token := range tokens{ | |
elem, ok := m[token] | |
if ok { | |
m[token] = elem + 1 | |
} | |
if !ok { | |
m[token] = 1 | |
} | |
} | |
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