Find frequency of each word in a string in C#

Finding the frequency of each word in a string in C# involves splitting the string into individual words and counting their occurrences. This is a common text processing task useful for data analysis, word clouds, and text mining applications.

Using Dictionary for Word Frequency

The most efficient approach is to use a Dictionary<string, int> to store words as keys and their frequencies as values −

using System;
using System.Collections.Generic;

class WordFrequency {
   public static void Main() {
      string text = "apple banana apple orange banana apple";
      Dictionary<string, int> wordCount = new Dictionary<string, int>();
      
      string[] words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
      
      foreach (string word in words) {
         string cleanWord = word.ToLower();
         if (wordCount.ContainsKey(cleanWord)) {
            wordCount[cleanWord]++;
         } else {
            wordCount[cleanWord] = 1;
         }
      }
      
      foreach (var pair in wordCount) {
         Console.WriteLine($"Word: {pair.Key}, Frequency: {pair.Value}");
      }
   }
}

The output of the above code is −

Word: apple, Frequency: 3
Word: banana, Frequency: 2
Word: orange, Frequency: 1

Using LINQ for Word Frequency

LINQ provides a more concise approach using GroupBy and Count methods −

using System;
using System.Linq;

class WordFrequencyLINQ {
   public static void Main() {
      string text = "hello world hello programming world hello";
      
      var wordFrequencies = text.Split(' ', StringSplitOptions.RemoveEmptyEntries)
                                .Select(word => word.ToLower())
                                .GroupBy(word => word)
                                .Select(group => new { Word = group.Key, Count = group.Count() });
      
      foreach (var item in wordFrequencies) {
         Console.WriteLine($"Word: {item.Word}, Frequency: {item.Count}");
      }
   }
}

The output of the above code is −

Word: hello, Frequency: 3
Word: world, Frequency: 2
Word: programming, Frequency: 1

Handling Punctuation and Case Sensitivity

For more robust text processing, you should handle punctuation marks and normalize case −

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class AdvancedWordFrequency {
   public static void Main() {
      string text = "Hello, world! Hello programming. World of programming!";
      Dictionary<string, int> wordCount = new Dictionary<string, int>();
      
      // Remove punctuation and split into words
      string cleanText = Regex.Replace(text.ToLower(), @"[^\w\s]", "");
      string[] words = cleanText.Split(' ', StringSplitOptions.RemoveEmptyEntries);
      
      foreach (string word in words) {
         if (wordCount.ContainsKey(word)) {
            wordCount[word]++;
         } else {
            wordCount[word] = 1;
         }
      }
      
      Console.WriteLine("Word frequencies:");
      foreach (var pair in wordCount.OrderByDescending(x => x.Value)) {
         Console.WriteLine($"{pair.Key}: {pair.Value}");
      }
   }
}

The output of the above code is −

Word frequencies:
hello: 2
programming: 2
world: 2
of: 1

Comparison of Methods

Method Advantages Best For
Dictionary Approach Simple, readable, good performance Basic word counting tasks
LINQ GroupBy Concise, functional style One-liner solutions, data analysis
Regex + Dictionary Handles punctuation, robust Real-world text processing

Conclusion

Finding word frequency in C# can be accomplished using Dictionary for manual counting, LINQ for concise operations, or regular expressions for robust text cleaning. Choose the Dictionary approach for simplicity, LINQ for functional programming style, or regex-based methods for handling complex text with punctuation and mixed cases.

Updated on: 2026-03-17T07:04:35+05:30

888 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements