[Solved] How To Get Count of element in List without linq [closed]


I am not sure why are you looking for LINQ less solution for this as this could be done very easily and efficiently by it. I strongly suggest you to use it and do it like below :

var _group = list.GroupBy(i => i);
string result = "";
foreach (var grp in _group)
   result += grp.Key + ": " + grp.Count() + Environment.NewLine;

MessageBox.Show(result);

Otherwise you can do it like below if you really unable to use LINQ :

Dictionary<string, int> listCount = new Dictionary<string, int>();
foreach (string item in list)
    if (!listCount.ContainsKey(item))
        listCount.Add(item, 1);
    else
        listCount[item]++;

string result2 = "";
foreach (KeyValuePair<string, int> item in listCount)
    result2 += item.Key + ": " + item.Value + Environment.NewLine;

MessageBox.Show(result2);

1

solved How To Get Count of element in List<> without linq [closed]