This is not possible in the dictionary. The dictionary is associative (i.e., each key must point to only one and one) and is inherently disordered. You will need to use something else for this data structure. It would be terribly difficult!
Edit
A List<Tuple<string, int>> should do the trick:
List<KeyValuePair<string, int>> structure = new List<KeyValuePair<string, int>>(); structure.Add(new KeyValuePair<string, int>(myList[0], 1); for(int i = 0; i < myList.Count; i++ ) { if( myList[i] == structure[structure.Count-1].Key ) { structure[structure.Count-1].Value += 1; } else { structure.Add(new KeyValuePair<string, int>(myList[i], 1); } }
After that, you should (unverified!) Have what you are looking for.
Change (another thought)
Although this is possible with linq (using TakeWhile and counts ...), I still think it makes sense to use a loop here, it's simple. Someone brighter than I could try and work with Linq.
Crisfole
source share