typestar

Word count with map in Go

Tallies word frequencies in a map, Go's built-in hash table.

func wordCount(text string) map[string]int {
    counts := make(map[string]int)
    for _, word := range strings.Fields(text) {
        counts[strings.ToLower(word)]++
    }
    return counts
}

Keywords and builtins used here

The run, in numbers

Lines
7
Characters to type
169
Tokens
49
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 33 seconds.

Type this snippet

Step 9 of 9 in Arrays, slices & maps, step 24 of 30 in Language basics.

← Previous Next →