Advertisement
Guest User

Solution for challenge "Using Dictionaries"

a guest
Jan 1st, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace Treehouse.CodeChallenges
  4. {
  5.     public class LexicalAnalysis
  6.     {
  7.         public Dictionary<string, int> WordCount = new Dictionary<string, int>();
  8.  
  9.         public void AddWord(string word)
  10.         {
  11.             if (WordCount.ContainsKey(word))
  12.             {
  13.                 WordCount[word]++;
  14.             }
  15.             else
  16.             {
  17.                 WordCount.Add(word, 1);
  18.             }
  19.         }
  20.         // Set the return type of the method to match the dictionary that will be returned
  21.         public Dictionary<string, int> WordsWithCountGreaterThan (int num){
  22.             // Create a new dictionary to hold the word with a count greater than num
  23.             Dictionary<string, int> greaterWord = new Dictionary<string, int>();
  24.             // loop though each item in the WordCount dictionary
  25.             foreach (KeyValuePair<string,int> word in WordCount)
  26.             {
  27.                 // Check if the value (count) of the word is greater than num
  28.                 if (word.Value > num)
  29.                 {
  30.                     // Add the word and it's value to the new dictionary if the word's count is greater than num
  31.                     greaterWord.Add(word.Key, word.Value);
  32.                 }
  33.             }
  34.             // Return the new dictionary
  35.             return greaterWord;
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement