日期:2014-05-19  浏览次数:20765 次

如何提取数组的交集
我有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