How to find if an element of a list is in another list?

前端 未结 5 467
误落风尘
误落风尘 2020-12-29 04:43

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<         


        
相关标签:
5条回答
  • 2020-12-29 04:54

    The second one has better performance on large lists than the first one. Intersect puts the elements of one list into a hash table before checking the other list's elements for membership.

    0 讨论(0)
  • 2020-12-29 04:54

    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<string> set2=new HashSet<string>(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.

    0 讨论(0)
  • 2020-12-29 05:08

    It seems odd to critique the performance of LINQ when the original is clearly (worst case) O(n*m); the LINQ approach would I expect use a HashSet<T> on a list, and then use a streaming iterator block - so the performance should be O(n+m) - i.e. better.

    0 讨论(0)
  • 2020-12-29 05:10

    The accepted answer is great, however it does not work with Linq-to-sql, since there's no mapping for Intersect. In that case you should use :

    bool isFound = table.Any(row => list2.Contains(row.FieldWithValue));
    

    This gets compiled to WHERE EXSITS

    0 讨论(0)
  • 2020-12-29 05:14

    This is another way to know if an element of one list exists in another list.

    bool present = List1.Any(t => List2.Any(y => y == t));
    
    0 讨论(0)
提交回复
热议问题