- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace 重复数字统计
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] s = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 79, 23, 45, 64, 9, 3, 2, 4 };
- Dictionary<int, int> dic = new Dictionary<int, int>();//将数组元素作为key 出现次数作为value
- for (int i = 0; i < s.Length; i++)
- {
- if (!dic.ContainsKey(s[i]))//如果里面没有这个数字,就将其存入dic
- {
- dic.Add(s[i], 1);
- }
- else//如果里面有就将其value+1
- {
- dic[s[i]]++;
- }
- }
- foreach (KeyValuePair<int, int> item in dic)
- {
- if (item.Value != 1)
- {
- Console.WriteLine("{0}出现了{1}次", item.Key, item.Value);
- }
- }
- }
- }
- }
复制代码 |