c# count number of element occurrences in list
C#[Edit]
+
0
-
0
c# count number of element occurrences in List
1 2 3 4 5 6 7 8List<string> myList = new List<string> { "A", "B", "B", "C", "C", "C" }; var occurrences = myList.GroupBy(x => x).ToDictionary(y => y.Key, z => z.Count()); // Output: // [A, 1] // [B, 2] // [C, 3]
[Edit]
+
0
-
0
c# count number of element occurrences in List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22using System; using System.Linq; using System.Collections.Generic; public class Program { public static void Main() { List<string> myList = new List<string> { "A", "B", "B", "C", "C", "C" }; var occurrences = myList.GroupBy(x => x).ToDictionary(y => y.Key, z => z.Count()); foreach (var item in occurrences) Console.WriteLine(item); } } // Output: // Output: // [A, 1] // [B, 2] // [C, 3]