Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package letter

import "sync"

// FreqMap records the frequency of each rune in a given text.
type FreqMap map[rune]int

// Frequency counts the frequency of each rune in a given text and returns this
// data as a FreqMap.
func Frequency(text string) FreqMap {
frequencies := FreqMap{}
for _, r := range text {
frequencies[r]++
}
return frequencies
}

// ConcurrentFrequency counts the frequency of each rune in the given strings,
// by making use of concurrency.
func ConcurrentFrequency(texts []string) FreqMap {
var wg sync.WaitGroup
var mu sync.Mutex

m := make(FreqMap)
for _, line := range texts {
wg.Add(1)
go func(line string) {
defer wg.Done()

for _, r := range line {
mu.Lock()
m[r]++
mu.Unlock()
}
}(line)
}

wg.Wait()

return m
}