1

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#?

balazs
  • 520
  • 4
  • 16

3 Answers3

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

HashSet.IntersectWith Method

For comparing two List I would create HashSet of the larger

HashSet Constructor (IEnumerable)

paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

Using Except

Difference between two lists

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234