

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a HashSet and a specified collection share common element in C#
To check if a HashSet and a specified collection share a common element, the C# code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<int> set1 = new HashSet<int>(); set1.Add(25); set1.Add(50); set1.Add(75); set1.Add(100); set1.Add(125); set1.Add(150); 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(100); set2.Add(150); set2.Add(200); set2.Add(250); Console.WriteLine("Elements in HashSet2"); foreach(int val in set2){ Console.WriteLine(val); } Console.WriteLine("Do they share common elements? "+set1.Overlaps(set2)); } }
Output
This will produce the following output −
Elements in HashSet1 25 50 75 100 125 150 Elements in HashSet2 30 60 100 150 200 250 Do they share common elements? True
Example
Let us now see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ HashSet<String> set1 = new HashSet<String>(); set1.Add("Nathan"); set1.Add("Tim"); set1.Add("Tom"); set1.Add("Jack"); set1.Add("Steve"); set1.Add("David"); Console.WriteLine("Elements in HashSet1"); foreach(string val in set1){ Console.WriteLine(val); } HashSet<String> set2 = new HashSet<String>(); set2.Add("Tom"); set2.Add("Jack"); set2.Add("Kevin"); Console.WriteLine("Elements in HashSet2"); foreach(string val in set2){ Console.WriteLine(val); } Console.WriteLine("Do they share common elements? "+set1.Overlaps(set2)); } }
Output
This will produce the following output −
Elements in HashSet1 Nathan Tim Tom Jack Steve David Elements in HashSet2 Tom Jack Kevin Do they share common elements? True
- Related Questions & Answers
- Check if SortedSet and a specified collection share common elements in C#
- Check if a HashSet contains the specified element in C#
- Check if a HashSet is a subset of the specified collection in C#
- Check if a HashSet is a superset of the specified collection in C#
- Check if a Java HashSet Collection contains another Collection
- Check if a HashSet is a proper subset of the specified collection in C#
- Check if a HashSet is a proper superset of the specified collection in C#
- Check if HashSet and the specified collection contain the same elements in C#
- Java Program to check if a particular element exists in HashSet
- Remove the specified element from a HashSet in C#
- Check if a SortedSet is a subset of the specified collection in C#
- Check if a SortedSet is a superset of the specified collection in C#
- Remove specified element from HashSet in Java
- Check if a SortedSet object is a proper subset of the specified collection in C#
- Check if a SortedSet object is a proper superset of the specified collection in C#
Advertisements