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

i need to understand this task

i tray to add the work to Dictionary

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)
        {
            int num=0;
           WordCount.Add(word , num);
            num++;
        }
    }
}

if(WordCount.ContainsKey(word)) { WordCount[word] += 1; } else { WordCount.Add(word, 1); }

1 Answer

Steven Parker
Steven Parker
229,732 Points

You won't always add to the dictionary. First check if the word is already there, and if so, just change the count value associated with it. Add the word only if the word is not already there.

And when you add a word, start the count off at 1 instead of 0, so you count the word that caused it to be added.

i tray many code to solve it like use try and use if but i need to know how to solve this problem

Steven Parker
Steven Parker
229,732 Points

Your code needs to include these steps:

  • check if the word is already in the dictionary
  • if it is, raise the count value by one
  • if it is not, add it to the dictionary with a count of 1

After you have code that should perform all of those steps, if it's still not working start a new question.