C#程序查找整数数组中的所有重复元素
首先,使用重复元素设置数组。
int[] arr = {
24,
10,
56,
32,
10,
43,
88,
32
};现在声明一个Dictionary并遍历数组以获取重复的元素。
var d = new Dictionary < int, int > ();
foreach(var res in arr) {
if (d.ContainsKey(res))
d[res]++;
else
d[res] = 1;
}示例
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = {
24,
10,
56,
32,
10,
43,
88,
32
};
var d = new Dictionary < int, int > ();
foreach(var res in arr) {
if (d.ContainsKey(res))
d[res]++;
else
d[res] = 1;
}
foreach(var val in d)
Console.WriteLine("{0} occurred {1} times", val.Key, val.Value);
}
}
}输出结果
24 occurred 1 times 10 occurred 2 times 56 occurred 1 times 32 occurred 2 times 43 occurred 1 times 88 occurred 1 times
热门推荐
10 春节后的祝福语简短
11 虎年公司祝福语简短的
12 中秋祝福语送朋友简短
13 晚儿子生日祝福语简短
14 早晨好正能量祝福语简短
15 分手祝福语简短短句
16 生日果酒祝福语简短独特
17 简短的小孩春节祝福语
18 高考送考祝福语简短