How to Find Intersection of Two Sets in C#
In C#, you can find the intersection of two sets using the
HashSet<T> class with its IntersectWith method or by using LINQ's Intersect method. Both approaches return the common elements between the two sets.Syntax
There are two common ways to find the intersection of two sets in C#:
- Using HashSet<T>: Create a
HashSet<T>from one collection, then callIntersectWithpassing the other collection. This modifies the original set to keep only common elements. - Using LINQ: Use the
Intersectextension method on one collection, passing the other. This returns a new sequence with common elements without modifying the originals.
csharp
HashSet<T> set1 = new HashSet<T>(collection1); set1.IntersectWith(collection2); // or using LINQ var intersection = collection1.Intersect(collection2);
Example
This example shows how to find the intersection of two sets of integers using both HashSet<int> and LINQ's Intersect. It prints the common numbers.
csharp
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var setA = new HashSet<int> { 1, 2, 3, 4, 5 }; var setB = new List<int> { 3, 4, 5, 6, 7 }; // Using HashSet.IntersectWith var hashSetIntersection = new HashSet<int>(setA); hashSetIntersection.IntersectWith(setB); Console.WriteLine("Intersection using HashSet.IntersectWith:"); foreach (var num in hashSetIntersection) { Console.WriteLine(num); } // Using LINQ Intersect var linqIntersection = setA.Intersect(setB); Console.WriteLine("\nIntersection using LINQ Intersect:"); foreach (var num in linqIntersection) { Console.WriteLine(num); } } }
Output
Intersection using HashSet.IntersectWith:
3
4
5
Intersection using LINQ Intersect:
3
4
5
Common Pitfalls
Common mistakes when finding intersections include:
- Modifying the original set unintentionally when using
IntersectWith. It changes the set it is called on. - Confusing
IntersectWith(in-place) with LINQ'sIntersect(returns new sequence). - Not considering case sensitivity or custom equality when working with complex types.
- Forgetting to include
using System.Linq;when using LINQ methods.
csharp
/* Wrong: modifies original set unexpectedly */ var set1 = new HashSet<int> {1, 2, 3}; var set2 = new List<int> {2, 3, 4}; set1.IntersectWith(set2); // set1 now only has 2,3 /* Right: use LINQ to keep originals intact */ var intersection = set1.Intersect(set2); // returns new IEnumerable<int>
Quick Reference
Summary tips for finding intersection of two sets in C#:
- Use
HashSet<T>.IntersectWithto modify a set in place. - Use LINQ
Intersectto get a new sequence without changing originals. - Remember to add
using System.Linq;for LINQ methods. - For custom objects, implement
IEqualityComparer<T>if needed.
Key Takeaways
Use HashSet.IntersectWith to find intersection by modifying the original set.
Use LINQ Intersect to get a new collection of common elements without changing originals.
Always include 'using System.Linq;' when using LINQ methods like Intersect.
Be careful with modifying collections in place versus returning new sequences.
Implement custom equality for complex types to get correct intersection results.