Count the words that matter
A support tool skims ticket text to see which terms keep coming up, ignoring the little joining words.
- Split on whitespace and compare in lowercase.
- Words shorter than three characters are noise and are left out.
- The result maps each remaining word to how many times it appeared.
wordFrequency(text: string) → map<string, int>
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func wordFrequency(text string) map[string]int {
}
Worked examples
| Call | Result |
|---|---|
wordFrequency("card declined card") | map[string]int{"card": 2, "declined": 1} |
wordFrequency("The the THE") | map[string]int{"the": 3} |
wordFrequency("a an I ok") | map[string]int{} |
wordFrequency("") | map[string]int{} |
Hint
Lowercase before counting, or "Payment" and "payment" end up as two entries.
Reference solution in Go
func wordFrequency(text string) map[string]int {
result := map[string]int{}
for _, w := range strings.Fields(strings.ToLower(text)) {
if len(w) < 3 {
continue
}
result[w]++
}
return result
}