Solution Review: Find Repeating Words in a Sentence

Understand the solution for the “Find Repeating Words in a Sentence” challenge.

We'll cover the following

Solution overview

For this challenge, you were required to write a function that takes in a string and outputs the repeating words in that string. The first thing to do is to split the string into individual words and we start by using the strings.Fields() function to split the input into separate words and store them in a slice called words.

There can be multiple ways to proceed further, and for this solution, we’ll create a map called wordCount to keep track of the count of each word. We’ll loop through each word and check whether it's already there in the map. If it is, we’ll increment the count by 1. Otherwise, we'll add that word to the map with the count value of 1. You can notice that we converted the words to lowercase before checking for their presence in the map.

We'll then loop through the map and store the words the repeating words, separated by commas, in a variable named wordlist. If there are no repeated words, an empty string will be returned.

Code

The complete code for this challenge is provided below.

Get hands-on with 1200+ tech skills courses.