Skip to content
Merged
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
43 changes: 43 additions & 0 deletions challenge-6/submissions/hudazaan/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package challenge6

import (
"strings"
"unicode"
)

// CountWordFrequency takes a string containing multiple words and returns
// a map where each key is a word and the value is the number of times that
// word appears in the string. The comparison is case-insensitive.
func CountWordFrequency(text string) map[string]int {
frequency := make(map[string]int)

if text == "" {
return frequency
}

var currentWord strings.Builder

for _, char := range text {
if unicode.IsLetter(char) || unicode.IsDigit(char) {
currentWord.WriteRune(unicode.ToLower(char))
} else if char == '\'' {
// ignore apostrophes (do nothing)
continue
} else {
// word boundary
if currentWord.Len() > 0 {
word := currentWord.String()
frequency[word]++
currentWord.Reset()
}
}
}

// handle last word if exists
if currentWord.Len() > 0 {
word := currentWord.String()
frequency[word]++
}

return frequency
}
Loading