patterncsharpModerate
Counting the number of character occurrences
Viewed 0 times
numberthecountingoccurrencescharacter
Problem
I've written some code that uses a
How can I improve this code? What should I be doing differently? Any advice at all would be helpful.
CharacterCount class:
Here is the output:
SortedDictionary to count the number of occurrences of a character in a string.How can I improve this code? What should I be doing differently? Any advice at all would be helpful.
static void Main(string[] args)
{
string longText = @"The quick brown fox jumps over the lazy dog";
var count = CharacterCount.Count(longText);
foreach (var character in count)
{
Console.WriteLine("{0} - {1}", character.Key, character.Value);
}
}CharacterCount class:
class CharacterCount
{
public static SortedDictionary Count(string stringToCount)
{
SortedDictionary characterCount = new SortedDictionary();
foreach (var character in stringToCount)
{
if (!characterCount.ContainsKey(character))
{
characterCount.Add(character, 1);
}
else
{
characterCount[character]++;
}
}
return characterCount;
}Here is the output:
Solution
You could do the same thing with LINQ:
For convenience/readability, you could turn this into an extension method on
And invoke it like this:
However, note that the above doesn't produce a sorted dictionary in the sense that if you were to add another key/value to it afterwards, the order would no longer be maintained. You could re-sort at that point, or use this
var counts = longText.GroupBy(c => c) // put each character into a "bucket"
// order the buckets alphabetically
.OrderBy(c => c.Key);
// then convert to dictionary where key = character, value = count
.ToDictionary(grp => grp.Key, grp => grp.Count())For convenience/readability, you could turn this into an extension method on
string:static class Extensions
{
public static Dictionary CharacterCount(this string text)
{
return text.GroupBy(c => c)
.OrderBy(c => c.Key)
.ToDictionary(grp => grp.Key, grp => grp.Count());
}
}And invoke it like this:
var longText = @"The quick brown fox jumps over the lazy dog";
var counts = longText.CharacterCount();However, note that the above doesn't produce a sorted dictionary in the sense that if you were to add another key/value to it afterwards, the order would no longer be maintained. You could re-sort at that point, or use this
SortedDictionary constructor to create a sorted dictionary.Code Snippets
var counts = longText.GroupBy(c => c) // put each character into a "bucket"
// order the buckets alphabetically
.OrderBy(c => c.Key);
// then convert to dictionary where key = character, value = count
.ToDictionary(grp => grp.Key, grp => grp.Count())static class Extensions
{
public static Dictionary<char, int> CharacterCount(this string text)
{
return text.GroupBy(c => c)
.OrderBy(c => c.Key)
.ToDictionary(grp => grp.Key, grp => grp.Count());
}
}var longText = @"The quick brown fox jumps over the lazy dog";
var counts = longText.CharacterCount();Context
StackExchange Code Review Q#63872, answer score: 13
Revisions (0)
No revisions yet.