How do you efficiently determine if any overlap exists between 2 .net lists
- User
How do you efficiently determine if any overlap exists between 2 .net lists
Assistant
Here are a few efficient ways to determine if any overlap exists between two lists in .NET:
- Use
List.Intersectmethod:
codeList<int> list1 = new List<int>{1, 2, 3, 4}; List<int> list2 = new List<int>{3, 4, 5, 6}; bool hasOverlap = list1.Intersect(list2).Any();- Use
HashSetintersection:
codeHashSet<int> set1 = new HashSet<int>(list1); HashSet<int> set2 = new HashSet<int>(list2); bool hasOverlap = set1.Overlaps(set2);- Loop through one list and check if each item exists in the other list using method:
- Use
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.