Counting number of repeating words in a Golang String


In many applications, it is important to count the number of times a word appears in a string. This can be useful for generating word frequency counts, analyzing text data, and many other tasks. In this article, we will explore how to count the number of repeating words in a Golang string.

Step 1: Convert the String to an Array of Words

The first step in counting repeating words is to convert the string into an array of words. This can be done using the strings.Split() function, which splits a string into an array of substrings based on a separator.

For example, consider the following string −

str := "the quick brown fox jumps over the lazy dog"

To convert this string into an array of words, we can use the following code −

words := strings.Split(str, " ")

This will split the string into an array of words, where each element in the array is a separate word.

Step 2: Create a Map to Store Word Counts

The next step is to create a map to store the count of each word. In Golang, a map is a built-in data structure that allows us to store key-value pairs. We can use a map to store the count of each word, where the key is the word and the value is the count.

We can create an empty map using the following code −

wordCount := make(map[string]int)

This will create an empty map with string keys and integer values.

Step 3: Count the Number of Occurrences of Each Word

Once we have the array of words and the empty map, we can iterate over each word in the array and increment the count of the corresponding key in the map.

for _, word := range words {
   _, exists := wordCount[word]
   if exists {
      wordCount[word] += 1
   } else {
      wordCount[word] = 1
   }
}

This code iterates over each word in the words array and checks if the word exists as a key in the wordCount map. If the word exists, it increments the count of the key by one. If the word does not exist, it adds the word as a new key to the map with a count of one.

Step 4: Print the Results

Finally, we can print the results of the word count. We can iterate over the keys and values in the wordCount map using a for loop and print each key-value pair.

for key, value := range wordCount {
   fmt.Printf("%s: %d\n", key, value)
}

This code iterates over each key-value pair in the wordCount map and prints the key and value to the console.

Putting It All Together

Here's the complete code for counting repeating words in a Golang string −

Example

package main

import (
   "fmt"
   "strings"
)

func main() {
   str := "the quick brown fox jumps over the lazy dog"
   words := strings.Split(str, " ")
   wordCount := make(map[string]int)

   for _, word := range words {
      _, exists := wordCount[word]
      if exists {
         wordCount[word] += 1
      } else {
         wordCount[word] = 1
      }
   }

   for key, value := range wordCount {
      fmt.Printf("%s: %d\n", key, value)
   }
}

Output

brown: 1
fox: 1
jumps: 1
over: 1
lazy: 1
dog: 1
the: 2
quick: 1

Conclusion

Counting the number of repeating words in a Golang string can be a useful technique for many applications. By splitting the string into an array of words and using a map to store the count of each word, we can quickly

Updated on: 07-Apr-2023

742 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements