What are some performant ways to determine the number of common elements between two lists or hashsets, which may have millions of values, in c#?
Asked
Active
Viewed 1,926 times
3 Answers
7
The best performance is going to be available with HashSets. You can use the IntersectWith method.
// assuming HashSet<T> hashSetA
// and an IEnumerable<T> collectionB
hashSetA.IntersectWith(collectionB);
The hashset-based solution gives O(n) performance which is pretty much as good as it gets.
The next best thing would be to sort the two lists and then iterate linearly in lock-step over the two lists, selecting the common elements, which yeilds O(nlogn) performance.

egrunin
- 24,650
- 8
- 50
- 93

Mike Dinescu
- 54,171
- 16
- 118
- 151
1
HashSet IntersectWith
For comparing two List I would create HashSet of the larger

paparazzo
- 44,497
- 23
- 105
- 176
0
Using Except

Community
- 1
- 1

Ramesh Rajendran
- 37,412
- 45
- 153
- 234
-
I believe you have the functions confused with Java.. The question is about C#. – Mike Dinescu Mar 28 '13 at 20:02
-
-
I'm sorry. I'm just not following. The `retainAll` function is not defined on any BCL class in .NET.. – Mike Dinescu Mar 28 '13 at 20:12