读书人

怎么提取数组的交集

发布时间: 2012-01-14 20:02:35 作者: rapoo

如何提取数组的交集
我有n个数组, 怎么提取这些数组的交集呢?

[解决办法]
这样可以吗?
private void button1_Click(object sender, EventArgs e)
{
int[] s1 = new int[7] { 1, 2, 3, 4, 5, 6, 7 };
int[] s2 = new int[5] { 3, 4, 5, 6, 7 };
int[] s3 = new int[8] { 5, 6, 7, 8, 9, 10, 11, 12 };

Dictionary <int, int> ht = new Dictionary <int, int> ();
PutValue(s1, ht);
PutValue(s2, ht);
PutValue(s2, ht);

foreach (int i in ht.Keys)
{
if (ht[i] == 3)
{
Console.WriteLine(i);
}
}
}

private void PutValue(int[] s, Dictionary <int, int> ht)
{
for (int i = 0; i < s.Length; i++)
{
if (!ht.ContainsKey(s[i]))
{
ht.Add(s[i], 1);
}
else
{
ht[s[i]]++;
}
}
}

//输出为:
5
6
7

读书人网 >C#

热点推荐