Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have a similar issue, but for word occurrences! so, I hope this can help. I used <code>SortedDictionary</code> and a binary search tree</p> <pre><code>/* Application counts the number of occurrences of each word in a string and stores them in a generic sorted dictionary. */ using System; using System.Text.RegularExpressions; using System.Collections.Generic; public class SortedDictionaryTest { public static void Main( string[] args ) { // create sorted dictionary SortedDictionary&lt; string, int &gt; dictionary = CollectWords(); // display sorted dictionary content DisplayDictionary( dictionary ); } // create sorted dictionary private static SortedDictionary&lt; string, int &gt; CollectWords() { // create a new sorted dictionary SortedDictionary&lt; string, int &gt; dictionary = new SortedDictionary&lt; string, int &gt;(); Console.WriteLine( "Enter a string: " ); // prompt for user input string input = Console.ReadLine(); // split input text into tokens string[] words = Regex.Split( input, @"\s+" ); // processing input words foreach ( var word in words ) { string wordKey = word.ToLower(); // get word in lowercase // if the dictionary contains the word if ( dictionary.ContainsKey( wordKey ) ) { ++dictionary[ wordKey ]; } else // add new word with a count of 1 to the dictionary dictionary.Add( wordKey, 1 ); } return dictionary; } // display dictionary content private static void DisplayDictionary&lt; K, V &gt;( SortedDictionary&lt; K, V &gt; dictionary ) { Console.WriteLine( "\nSorted dictionary contains:\n{0,-12}{1,-12}", "Key:", "Value:" ); /* generate output for each key in the sorted dictionary by iterating through the Keys property with a foreach statement*/ foreach ( K key in dictionary.Keys ) Console.WriteLine( "{0,- 12}{1,-12}", key, dictionary[ key ] ); Console.WriteLine( "\nsize: {0}", dictionary.Count ); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload