MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / CountChars

Function CountChars

strings/charoccurrence.go:12–21  ·  view source on GitHub ↗

CountChars counts the number of a times a character has occurred in the provided string argument and returns a map with `rune` as keys and the count as value.

(text string)

Source from the content-addressed store, hash-verified

10// has occurred in the provided string argument and
11// returns a map with `rune` as keys and the count as value.
12func CountChars(text string) map[rune]int {
13 charMap := make(map[rune]int, 0)
14 for _, c := range text {
15 if _, ok := charMap[c]; !ok {
16 charMap[c] = 0
17 }
18 charMap[c]++
19 }
20 return charMap
21}

Callers 1

TestCountCharsFunction · 0.92

Calls

no outgoing calls

Tested by 1

TestCountCharsFunction · 0.74