Word Frequency Counter

Count how many times each word appears using a map.

package main

import (
	"fmt"
	"strings"
)

func main() {
	text := "the cat sat on the mat the cat"
	words := strings.Split(text, " ")

	counts := map[string]int{}
	for _, word := range words {
		counts[word]++
	}

	for word, count := range counts {
		fmt.Printf("%s: %d\n", word, count)
	}
}
▶ Open Go Playground

Copy the code above and paste to run

© 2026 ByteLearn.dev. Free courses for developers. · Privacy