Word Frequency Dictionary
Count normalized words with Dictionary<TKey,TValue>.
Introduction
This exercise practices keyed lookup, normalization, and collection expressions.
GetValueOrDefault provides zero for unseen words before the count is incremented.
Runnable C# example
Program.cs
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var counts = new Dictionary<string, int>();
foreach (string word in "C# is fun and C# is fast".ToLower().Split(' '))
counts[word] = counts.GetValueOrDefault(word) + 1;
Console.WriteLine(counts["c#"]);
}
}
2Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Count Words Count the words in a sentence. Easy
- Character Frequency Count how often each character appears. Easy