随着LINQ,这是微不足道的,因为你可以调用Intersect
extension method上Enumerable
class给你两个数组的交集:
var intersection = ListA.Intersect(ListB);
然而,这是设置路口,这意味着如果ListA
和ListB
没有独特的值,你不会得到任何副本。换句话说,如果你有以下:
var ListA = new [] { 0, 0, 1, 2, 3 };
var ListB = new [] { 0, 0, 0, 2 };
然后ListA.Intersect(ListB)
生产:
{ 0, 2 }
如果您期望:
{ 0, 0, 2 }
然后你将不得不保持当您扫描两个列表时,您自己的项目数和收益/递减项。
首先,你要收集单个项目的列出了Dictionary<TKey, int>
:
var countsOfA = ListA.GroupBy(i => i).ToDictionary(g => g.Key, g => g.Count());
从那里,你可以扫描ListB
并放置在列表中,当你在countsOfA
遇到一个项目:
:
// The items that match.
IList<int> matched = new List<int>();
// Scan
foreach (int b in ListB)
{
// The count.
int count;
// If the item is found in a.
if (countsOfA.TryGetValue(b, out count))
{
// This is positive.
Debug.Assert(count > 0);
// Add the item to the list.
matched.Add(b);
// Decrement the count. If
// 0, remove.
if (--count == 0) countsOfA.Remove(b);
}
}
您可以在推迟执行,像这样的扩展方法包装这件事
注意,这两种方法是(和我道歉,如果我在这里屠宰大O符号)O(N + M)
其中N
是第一个数组中的项目数,并M
是项目的第二阵列中的数。您必须仅扫描一次每个列表,并且假定获取哈希代码并在哈希代码上执行查找是O(1)
(常量)操作。
不是BinarySearch依靠被排序的列表吗? http://msdn.microsoft.com/en-us/library/w4e7fxsh.aspx – 2009-01-07 06:45:21