I want to know if at least one element in a first list can be found in a second list.
I can see two ways to do it. Let\'s say our lists are:
List<
I think the second one will be faster for large lists. Since the first one is O(list1.Count*list2.Count) whereas the second is O(list1.Count+list2.Count). Second one takes more memory though.
And the overhead of linq is typically a constant multiplication factor over handcrafted code. I'd guess the second one is slower than imperative code by at most a factor of two, probably not even that. It uses O(list1.Count+list2.Count)
memory which can be cut down to O(Min(list1,list2))
if you carefully write your code for low memory usage whilte retaining linear performance.
This code should be relatively fast on large lists:
bool isFound = false;
HashSet set2=new HashSet(list2);
foreach (item1 in list1)
{
if (set2.Contains(item1))
{
isFound = true;
break;
}
}
You can optimize this code further by making the smaller list into a hashset instead of always using list2.