Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.

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.Add(word, 1);

1 Answer

Steven Parker
Steven Parker
221,585 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