在c#中,有哪些有效的方法来确定两个列表或哈希集(可能具有数百万个值)之间的公共元素数量?
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.
HashSet IntersectWith
For comparing two List I would create HashSet of the larger
Using Except
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.