As mentioned, using Any() will give you some performance.
I tested it on a fairly large dataset and it gave 25% improvement.
It is also very important to use larger.Intersect(smaller) , and not vice versa, in my case it gave 35% improvement.
In addition, when ordering a list before applying the intersection, another 7-8% is given.
Another thing to keep in mind is that depending on the use case, you can completely avoid the use of intersection.
For example, for an integer list, if the maximum and minimum do not fall within the same boundaries, you do not need to apply the intersection, since they will never do.
The same thing applies to a string list with the same idea applied to the first letter.
Again, depending on your case, try to find the rule as much as possible where the intersection cannot be avoided.
badr slaoui
source share