What is the fastest way to find common items in two C# lists?
Finding common items between two generic lists in C# can be a common requirement in many applications, particularly when you want to understand overlaps in datasets. Using the example provided, we will discover how to efficiently identify shared elements between two lists of strings: Understanding the Problem In this scenario, we have two generic lists: List TestList1 = new List(); List TestList2 = new List(); TestList1.Add("1"); TestList1.Add("2"); TestList1.Add("3"); TestList2.Add("3"); TestList2.Add("4"); TestList2.Add("5"); Our goal is to find the common elements in TestList1 and TestList2, which, in this case, should return "3". Efficient Methods to Find Common Items When it comes to finding common elements between two lists, performance is critical, especially when dealing with large datasets. The following sections describe a couple of methods to achieve this. Method 1: Using HashSet Using a HashSet is one of the fastest methods for finding intersections between two lists in C#. Here's how you can do it: Step 1: Initialize the HashSet First, you need to create a HashSet from one of the lists: HashSet set = new HashSet(TestList1); Step 2: Find Intersecting Items Then, you can use the Intersect method combined with ToList to get the common items: List commonItems = TestList2.Intersect(set).ToList(); Complete Code Example Here’s the complete code using this method: using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List TestList1 = new List(); List TestList2 = new List(); TestList1.Add("1"); TestList1.Add("2"); TestList1.Add("3"); TestList2.Add("3"); TestList2.Add("4"); TestList2.Add("5"); HashSet set = new HashSet(TestList1); List commonItems = TestList2.Intersect(set).ToList(); Console.WriteLine("Common Items: " + string.Join(", ", commonItems)); } } Method 2: Using LINQ Query Another great way to find common elements is through LINQ queries. This method also offers readability and conciseness: var commonItemsLINQ = TestList1.Where(x => TestList2.Contains(x)).ToList(); The complete usage of this approach looks like this: using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List TestList1 = new List() { "1", "2", "3" }; List TestList2 = new List() { "3", "4", "5" }; var commonItemsLINQ = TestList1.Where(x => TestList2.Contains(x)).ToList(); Console.WriteLine("Common Items using LINQ: " + string.Join(", ", commonItemsLINQ)); } } Comparing the Two Methods Both methods receive strong recommendations depending on the size of the lists and the context: HashSet: Recommended for large lists due to better performance with average time complexity of O(n). LINQ: Offers simplicity and is more readable, though can be less performant for larger datasets since it uses nested iteration. Best Practices Prefer HashSet for performance-intensive applications or larger datasets. Use LINQ for smaller or medium-sized datasets where readability is a concern. Frequently Asked Questions (FAQ) What if the lists contain duplicates? If your lists contain duplicates and you want unique common items, both methods will discard duplicates by default since a HashSet inherently holds only unique values and LINQ’s Where duplicates will also be combined. Can I find common elements with different types? For finding common elements between two lists of different types, you would first have to convert or cast those types into a common type that they can both be evaluated against. In summary, finding common items across two lists in C# can be efficiently done using either a HashSet or LINQ queries. Choose the method that fits best based on your specific use case!

Finding common items between two generic lists in C# can be a common requirement in many applications, particularly when you want to understand overlaps in datasets. Using the example provided, we will discover how to efficiently identify shared elements between two lists of strings:
Understanding the Problem
In this scenario, we have two generic lists:
List TestList1 = new List();
List TestList2 = new List();
TestList1.Add("1");
TestList1.Add("2");
TestList1.Add("3");
TestList2.Add("3");
TestList2.Add("4");
TestList2.Add("5");
Our goal is to find the common elements in TestList1
and TestList2
, which, in this case, should return "3".
Efficient Methods to Find Common Items
When it comes to finding common elements between two lists, performance is critical, especially when dealing with large datasets. The following sections describe a couple of methods to achieve this.
Method 1: Using HashSet
Using a HashSet
is one of the fastest methods for finding intersections between two lists in C#. Here's how you can do it:
Step 1: Initialize the HashSet
First, you need to create a HashSet
from one of the lists:
HashSet set = new HashSet(TestList1);
Step 2: Find Intersecting Items
Then, you can use the Intersect
method combined with ToList
to get the common items:
List commonItems = TestList2.Intersect(set).ToList();
Complete Code Example
Here’s the complete code using this method:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List TestList1 = new List();
List TestList2 = new List();
TestList1.Add("1");
TestList1.Add("2");
TestList1.Add("3");
TestList2.Add("3");
TestList2.Add("4");
TestList2.Add("5");
HashSet set = new HashSet(TestList1);
List commonItems = TestList2.Intersect(set).ToList();
Console.WriteLine("Common Items: " + string.Join(", ", commonItems));
}
}
Method 2: Using LINQ Query
Another great way to find common elements is through LINQ queries. This method also offers readability and conciseness:
var commonItemsLINQ = TestList1.Where(x => TestList2.Contains(x)).ToList();
The complete usage of this approach looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List TestList1 = new List() { "1", "2", "3" };
List TestList2 = new List() { "3", "4", "5" };
var commonItemsLINQ = TestList1.Where(x => TestList2.Contains(x)).ToList();
Console.WriteLine("Common Items using LINQ: " + string.Join(", ", commonItemsLINQ));
}
}
Comparing the Two Methods
Both methods receive strong recommendations depending on the size of the lists and the context:
- HashSet: Recommended for large lists due to better performance with average time complexity of O(n).
- LINQ: Offers simplicity and is more readable, though can be less performant for larger datasets since it uses nested iteration.
Best Practices
- Prefer
HashSet
for performance-intensive applications or larger datasets. - Use LINQ for smaller or medium-sized datasets where readability is a concern.
Frequently Asked Questions (FAQ)
What if the lists contain duplicates?
If your lists contain duplicates and you want unique common items, both methods will discard duplicates by default since a HashSet
inherently holds only unique values and LINQ’s Where
duplicates will also be combined.
Can I find common elements with different types?
For finding common elements between two lists of different types, you would first have to convert or cast those types into a common type that they can both be evaluated against.
In summary, finding common items across two lists in C# can be efficiently done using either a HashSet
or LINQ queries. Choose the method that fits best based on your specific use case!