I need to count the number of elements corresponding to the intersection of two big arrays of strings and do it very fast.
I am using the following code:
arr1[i].Intersect(arr2[j]).Count()
For CPU Time, VS Profiler indicates
System.Linq.Enumerable.Count()
System.Linq.Enumerable.Intersect()
Unfortunately it might to take hours to do all work.
How to do it faster?
You can use HashSet
with arr2
HashSet<string> arr2Set = new HashSet<string>(arr2);
arr1.Where(x=>arr2Set.Contains(x)).Count();
------------------
|
|->HashSet's contains method executes quickly using hash-based lookup..
Not considering the conversion from arr2
to arr2Set
,this should be O(n)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With