首先,设置一个数组-
int[] arr = { 87, 55, 23, 87, 45, 23, 98 };
现在声明一个字典并遍历数组,并获取所有元素的计数。您从字典中获得的值显示数字的出现-
foreach(var count in arr) { if (dict.ContainsKey(count)) dict[count]++; else dict[count] = 1; }
让我们看完整的例子-
using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 87, 55, 23, 87, 45, 23, 98 }; var dict = new Dictionary < int, int > (); foreach(var count in arr) { if (dict.ContainsKey(count)) dict[count]++; else dict[count] = 1; } foreach(var val in dict) Console.WriteLine("{0} occurred {1} times", val.Key, val.Value); } } }