so i have 4 list of words. what i want to do is for each list go through it and count the number of occurance the word has in the list and print the first 10.

when it comes to saving the word and the number of occurance, how do i do it. because the number of words in each list is different. how do i make an array for a unknow number of words?

i need to save the word and the number of count for that waord also...

aprreciate a reply

Recommended Answers

All 15 Replies

Look up Dictionary on MSDN.

Or look up List<string> in the MSDN library.

is there a way to get the index of the n'th element????

Rather strange question. I guess that index would be n. But look up the different Find methods in the Dictionary or List classes.

i ask the above question because if i compare the string in the lsit and find a match i need to get the index number to update it in the other List<> at the same index

there is a method to get the value of index n, but no the other way around

hey ddanbe,

why si it giving an error under the words Text, says 'string does not aontain definition for text'

 List<string> words = new List<string>();
                        List<string> wordCount = new List<string>();
                        foreach (string w in words)
                            if (w.Equals(value))
                                int result = words.FindIndex(p => p.Text(w) == value);

                                v = false;



int i = words.IndexOf(w);

So you are maintaining two lists; one for the words and the second to to strore the number of occurence of that word.

Why not use the dictionary class as was previously suggested by ddanbe?
It has all the functionallity you require already built-in.

private Dictionary<string, Int32> words = new Dictionary<string, Int32>();

Then you could do something like this:

      string subjectword = "fred";  // the current word you are working with

      if (words.ContainsKey(subjectword))
         words[subjectword] += 1;
         words.Add(subjectword, 1);

thanks i didn't understand it

how to i sort the Dictionary with the highest number of occurance in the top by it should be alpabatical order,


hey, if i change it to SortedDictionary<int, string> words = new SortedDictionary<int, string>();

why is "words[subjectword] += 1;" not working???

gives an error sating sorteddictionary<int,string>.this[int] has some invalid arguments


I think you have it backwards... The string should be the key in this case (i.e. SortedDictionary<string, int>), since there is a one-to-many relationship between the key and the value. If you did it with SortedDictionary<int, string> you could never have words that equal the same amount.

how do i sort it so the the value has the higher number of count and the key alphabatical order

i used the blow, but it sorts according only to the key

            SortedDictionary<string, int>.KeyCollection SortedkeyColl = words.Keys;
            foreach (string i in SortedkeyColl)
                Console.WriteLine("value = {0}", i);

how do i sort so that the value has the highest number and the value according to alpabatical???

 SortedDictionary<string, int> words = new SortedDictionary<string, int>();
            foreach (KeyValuePair<string, int> item in words.OrderByDescending(key => key.Value))
                // do something with item.Key and item.Value
                Console.WriteLine("{0}, {1} ", item.Key, item.Value);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.