0

I'm looking to find out how I can make a counter for the occurrence of two words in the same order in a list. As the words can vary (I am using Console.ReadLine to input them) I cannot figure out what to do. My list format is as for example:

list[0] : list[1] (1) //this is where i want count
list[1] : list[2](1)

so say the words were:

Hello : I
am : John
Hello : I


![when the program runs](http://imgur.com/HelGjxV)

Btw the second sentence written is what I typed. when the second Hello : I is entered to the list. how could I go about setting the counter (1) to update to 2. Any help would be greatly appreciated. I'll include my relevant code thus far :

List<string> list = new List<string>();
list.AddRange(words);
list.AddRange(words1);

//counts total number of words in list. 
int count = 0;
count  = list.Count(d => list.Contains(d));

//Displays occurance
for (int i = 0; i < list.Count - 1; i++)
    list[i] = string.Format("{0} : {1}", list[i], list[i + 1]);

list.ForEach(Console.WriteLine);

2 Answers2

0

You can use the Dictionary for these kind of opearion

Dictionary<string,List<int>> dict = new Dictionary<string,List<int>>();
if(dict.ContainsKey(wordkey))
    //update the key
    dict[word].Add(newIndex);
else
{
    var newList = new List();
    newList.Add(index);
    dict.Add(worditem,newList);
}
Noctis
  • 11,507
  • 3
  • 43
  • 82
Devesh
  • 4,500
  • 1
  • 17
  • 28
0

List(T).FindAll method is what you want.

Example code is available here http://msdn.microsoft.com/en-us/library/fh1w7y8z(v=vs.110).aspx

Ken Thai
  • 76
  • 4