Counting List Occurrences in C#
This inquiry seeks a method in C# to count the frequency of each element within a list and add those counts back into the list.
Solution:
Consider the following approach, which leverages the GroupBy method:
var l1 = new List<int>() { 1,2,3,4,5,2,2,2,4,4,4,1 }; var g = l1.GroupBy(i => i); foreach (var grp in g) { Console.WriteLine("{0} {1}", grp.Key, grp.Count()); }
Explanation:
The above is the detailed content of How to Count List Element Occurrences and Add Counts Back to the List in C#?. For more information, please follow other related articles on the PHP Chinese website!