How to get frequency of elements from List in c#

c#, frequency, list

Solution

using System.Linq;

List<int> ids = //

foreach(var grp in ids.GroupBy(i => i))
{
    Console.WriteLine("{0} : {1}", grp.Key, grp.Count());
}

Problem

I am trying to get the frequency of elements stored in a list. I am storing the following ID's in my list ``` ID 1 2 1 3 3 4 4 4 ``` I want the following output: ``` ID| Count 1 | 2 2 | 1 3 | 2 4 | 3 ``` In java you can do the following way. ``` for (String temp : hashset) { System.out.println(temp + ": " + Collections.frequency(list, temp)); } ``` Source:http://www.mkyong.com/java/how-to-count-duplicated-items-in-java-list/ How to get the frequency count of a list in c#? Thanks.

Original source