Sort words by letter count in C#

[letter count]

This example sorts words by letter count. It counts the number of distinct letters in a list of words and displays the words and their counts sorted by the counts. The program is remarkably simple.

The following code shows how the program builds and displays the letter count list.

// Find and display the words and counts.
private void Form1_Load(object sender, EventArgs e)
    string[] words = {

    // Get a list holding each word's unique letter count and name.
    var count_query =
        from string word in words
        orderby word.ToCharArray().Distinct().Count()
        select word.ToCharArray().Distinct().Count() + ", " + word;
    lstLetterCounts.DataSource = count_query.ToArray();

The code first defines an array of words. It then builds the LINQ query that does most of the work.

The query loops through the words in the array. For each word, it calls the string class’s ToCharArray method to convert the word into an array of characters. It then calls Distinct to get the distinct letters in the array. Finally it calls Count to see how many distinct letters there are. It uses the orderby clause to order the results by this value.

The query selects the same letter count, plus a comma, plus the original word. The result is that the query returns strings holding each word’s distinct letter count and the word.

To display the results, the code simply calls the query’s ToArray method to convert the IEnumerable list of strings into an array. It then sets the ListBox control’s DataSource property equal to the resulting array and the ListBox displays the strings.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, LINQ, mathematics and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Sort words by letter count in C#

  1. Eddie Bole says:

    Made something similar in vb6 to count unique word occurrences. I’ve been reading a bit about linq queries and how powerful they can be. Thanks for the example. How can this example be altered to include words from a textbox or from a file? I know in vb6, it’s just adding the words by splitting into arrays. Another good feature maybe to create a histogram in a scrollable window to show the frequencies in graphical form. Something like this could possible used to teach English, to see which words have been over used in say an essay.

    • RodStephens says:

      To use the words in a text box, you could use Split to split the text into words. Then use LINQ as before.

      var count_query =
          from string word in txtWords.Text.Split(' ')
          orderby word.ToCharArray().Distinct().Count()
          select word.ToCharArray().Distinct().Count() + ", " + word;

      If you want to include words from multiple sources, you would need to combine them. One way would be to make a List<string> and use its AddRange method to add each of the word sources. For example, the following would add the words in the txtWords text box.

      List<string> all_words = new List<string>();
      all_words.AddRange(txtWords.Text.Split(' '));

      After you finishing adding all of the sources to the list, use the list in the LINQ query.

      If you search my index page for "histogram," you'll find some histogram examples.

Comments are closed.