stephen reilly February 2016
### how to calculate the number of common integers between two sorted sets

I need to count the number of integers between two sorted sets to determine if a lotto ticket is a winner. One sorted set being winningNumbers and the other ticketNumbers. I was told there is a intersect function but I cannot find a function to achieve the required results. Ideally I would like a function that returns an int representing the number of common integers between the sets.

Even if it's not sorted this works:

```
IEnumerable<int> winning = winningNumbers.Intersect(ticketNumbers);
int countOfWinningNumbers = winning.Count();
```

If you want to process it further it would be better to create a collection:

```
List<int> winningList = winning.ToList();
int countOfWinningNumbers = winningList.Count;
```

Since you've asked explicitly for a `SortedSet`

approach, using `SortedSet.IntersectWith`

might be more efficient(*O(n)*) but modifies the source set:

```
SortedSet<int> winningNumbers = new SortedSet<int> { 2, 3, 7 };
SortedSet<int> ticketNumbers = new SortedSet<int> { 1, 2, 3, 4, 5 };
ticketNumbers.IntersectWith(winningNumbers); // now ticketNumbers contains only 2 and 3
```

Asked in February 2016

Viewed 1,182 times

Voted 7

Answered 1 times

Viewed 1,182 times

Voted 7

Answered 1 times