
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Intersection of two HashSets in C#
To find the intersection of two HashSets, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<string> set1 = new HashSet<string>(); set1.Add("AB"); set1.Add("CD"); set1.Add("EF"); set1.Add("AB"); set1.Add("IJ"); set1.Add("KL"); set1.Add("EF"); set1.Add("OP"); Console.WriteLine("Elements in HashSet1"); foreach(string val in set1){ Console.WriteLine(val); } HashSet<string> set2 = new HashSet<string>(); set2.Add("EF"); set2.Add("KL"); Console.WriteLine("
Elements in HashSet2"); foreach(string val in set2){ Console.WriteLine(val); } Console.WriteLine("Count of elements in HashSet2 = " + set2.Count); set2.Remove("KL"); Console.WriteLine("
Elements in HashSet2... (UPDATED)"); foreach (string res in set2){ Console.WriteLine(res); } Console.WriteLine("Count of elements in HashSet2 (Updated) = " + set2.Count); Console.WriteLine("
Is set1 a superset of set2? "+set1.IsSupersetOf(set2)); set1.IntersectWith(set2); Console.WriteLine("
Intersection result..."); foreach(string str in set1){ Console.WriteLine(str); } } }
Output
This will produce the following output −
Elements in HashSet1 AB CD EF IJ KL OP Elements in HashSet2 EF KL Count of elements in HashSet2 = 2 Elements in HashSet2... (UPDATED) EF Count of elements in HashSet2 (Updated) = 1 Is set1 a superset of set2? True Intersection result... EF
Example
Let us now see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(30); set1.Add(60); set1.Add(70); set1.Add(80); set1.Add(100); set1.Add(125); Console.WriteLine("Elements in HashSet1"); foreach(int val in set1){ Console.WriteLine(val); } HashSet<int> set2 = new HashSet<int>(); set2.Add(30); set2.Add(60); set2.Add(70); set2.Add(80); set2.Add(100); set2.Add(125); Console.WriteLine("
Elements in HashSet2"); foreach(int val in set2){ Console.WriteLine(val); } set1.IntersectWith(set2); Console.WriteLine("
Intersection result..."); foreach(int val in set1){ Console.WriteLine(val); } } }
Output
This will produce the following output −
Elements in HashSet1 30 60 70 80 100 125 Elements in HashSet2 30 60 70 80 100 125 Intersection result... 30 60 70 80 100 125
- Related Articles
- Intersection of two arrays in Java
- Intersection of two arrays in C#
- Intersection of Two Arrays in C++
- Intersection of two arrays JavaScript
- Python - Intersection of two String
- Intersection of Two Arrays II in Python
- Intersection of Two Linked Lists in Python
- Intersection of Two Linked Lists in C++
- Get the intersection of two sets in Java
- Explain the intersection process of two DFA’s
- Program for Point of Intersection of Two Lines in C++
- Get intersection between two ranges in JavaScript
- C# program to find Intersection of two lists
- Python program to find Intersection of two lists?
- How to get the intersection of two arrays in MongoDB?

Advertisements