KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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<string> list1 = new[] { "A", "C", "F", "H", "I" }; List<string> list2 = new[] { "B", "D", "F", "G", "I" }; The first approach uses a loop: bool isFound = false; foreach (item1 in list1) { if (list2.Contains(item1)) { isFound = true; break; } } The second one uses Linq directly: bool isFound = list1.Intersect(list2).Any(); The first one is long to write and not very straightforward/easy-to-read. The second one is short and clear, but performances will be low, especially on large lists. What may be an elegant way to do it?
Tags (comma-separated)
Save Edits
Cancel