Skip to content
Exercise · Maps · lesson 8 of 9

Exercise: word frequency

Write a function func wordCount(text string) map[string]int that returns a map whose keys are the words of text and whose values are how many times each word appears.

  • wordCount("go is go") → map[string]int{"go": 2, "is": 1}
  • wordCount("one") → map[string]int{"one": 1}
  • wordCount("") → an empty map

Constraints: words are separated by one or more spaces, and spaces can also come at the start and the end of the text; case matters, so Go and go are different words; for an empty text, the function returns an empty map.

Do not add extra imports unless the task says so explicitly.

words.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way