Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Sets and Dictionaries Using Dictionaries

whats wrong with this code help pliz

Challenge Task 1 of 2

Code the AddWord method such that, WordCount will contain the number of times the AddWord method has been called with a given word.

For example, if AddWord is called twice with the word "rock" then WordCount["rock"] will be 2.

LexicalAnalysis.cs
using System.Collections.Generic;

namespace Treehouse.CodeChallenges
{
    public class LexicalAnalysis
    {
        public Dictionary<string, int> WordCount = new Dictionary<string, int>();

        public void AddWord(string word)
        {
            if (WordCount.ContainsKey(word))
            {
                WordCount[word]++;
            }
            else
            {
                WordCount.Add(word, 1);
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,732 Points

It looks like you did a good job on task 1 :+1:

For task 2, here's a few hints about what your WordsWithCountGreaterThan method should have:

  • it should create a new Dictionary variable
  • it should have a loop to iterate the Wordcount
  • it should compare the count of each word to the passed-in argument
  • it should add both the word and the count to the new dictionary when the criterion is met
  • it should return the newly-created dictionary