← All Exercises

Go Map Basics

basicgo~15min

Background

Maps are Go built-in hash table type, providing O(1) average lookup, insertion, and deletion.

Problem

Write a Go function that counts word frequencies in a string and returns a map of word to count.

Input

A string of space-separated words.

Output

A map[string]int mapping each word to its occurrence count.

Sample tests

Sample 1

Input: "the quick brown fox the lazy fox"
Expected: {"the":2,"quick":1,"brown":1,"fox":2,"lazy":1}

Your solution

Learning objective

Learn map initialization, iteration with strings.Fields, and the zero-value access behavior.