Find the highest occuring words in a string C
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In C#, finding the highest occurring words within a string is a common task that can be efficiently handled using collections and LINQ. This article delves into the technical aspects of solving this problem using C# programming, complete with code examples and explanations. By the end of this article, you'll be equipped to implement a solution that identifies the most frequently occurring words in any given string.
Parsing the String
Splitting the String
The first step in identifying the highest occurring words is to parse the string into individual words. This can be achieved using the Split method. In typical scenarios, words are separated by spaces, but we also need to consider other delimiters such as punctuation marks. Here's a simple example:
Explanation
- We use an array of characters including a space, exclamation mark, period, and question mark as delimiters.
StringSplitOptions.RemoveEmptyEntriesremoves any empty entries from the resulting array.
Counting Word Occurrences
Using a Dictionary
After parsing the words, we need a way to count occurrences. For this purpose, a Dictionary<string, int> can be particularly useful. Here's how you can use it:
Explanation
- The words are converted to lowercase to ensure case insensitivity.
- If the word is already in the dictionary, its count is incremented.
- If not, the word is added with an initial count of 1.
Finding the Highest Occurring Words
Utilizing LINQ
Once we have a dictionary of word counts, LINQ can be employed to find the words with the highest frequency. Consider the following code snippet:
Explanation
Values.Max()retrieves the maximum count from the dictionary.- We filter (
Where) those entries whose value matches the maximum count. - Finally, we select only the keys (words) corresponding to these entries.
Full Example
Here’s the complete code that combines all the above pieces:
Summary Table
The table below summarizes the key steps involved in finding the highest occurring words in a string:
| Step | Description |
| Parse the String | Split the string into words using delimiters. |
| Count Occurrences | Use a dictionary to count the frequency of each word, converting words to lowercase for case insensitivity. |
| Find Maximum Count | Use LINQ to determine the maximum word count. |
| Identify Words | Filter and select words that have the maximum count. |
Conclusion
Finding the highest occurring words in a string involves parsing, counting, and filtering processes. By leveraging C# collections and LINQ, we can effectively and efficiently determine word frequencies and identify the most frequent words. This technique can be particularly useful in text analysis, data processing, and natural language processing applications.
Related reading
- Find the number of occurrences of a subsequence in a string
- Find the Number of Occurrences of a Substring in a String
- Finding all the common substrings of given two strings
- Finding head of a noun phrase in NLTK and stanford parse according to the rules of finding head of a NP
- Find the next TCP port in .NET
- Find type of nullable properties via reflection
- Finding how similar two strings are
- Finding meaningful sub-sentences from a sentence

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.