- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove all elements in a collection from a HashSet in C#
To remove all elements in a collection from a HashSet, the code is as follows −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ HashSet<string> set1 = new HashSet<string>(); set1.Add("Ryan"); set1.Add("Tom"); set1.Add("Andy"); set1.Add("Tim"); Console.WriteLine("Elements in HashSet1..."); foreach (string res in set1){ Console.WriteLine(res); } HashSet<string> set2 = new HashSet<string>(); set2.Add("John"); set2.Add("Jacob"); set2.Add("Ryan"); set2.Add("Tom"); set2.Add("Andy"); set2.Add("Tim"); set2.Add("Steve"); set2.Add("Mark"); Console.WriteLine("Elements in HashSet2..."); foreach (string res in set2){ Console.WriteLine(res); } Console.WriteLine("Is HashSet1 equal to HashSet2? = "+set1.Equals(set2)); set2.ExceptWith(set1); // displaying elements in set2, which are not on set1 foreach(string i in set2){ Console.WriteLine(i); } } }
Output
This will produce the following output −
Elements in HashSet1... Ryan Tom Andy Tim Elements in HashSet2... John Jacob Ryan Tom Andy Tim Steve Mark Is HashSet1 equal to HashSet2? = False John Jacob Steve Mark
Example
Let us see another example −
using System; using System.Collections.Generic; public class Demo { public static void Main(String[] args){ HashSet<string> set1 = new HashSet<string>(); set1.Add("Jacob"); set1.Add("Ryan"); set1.Add("Tom"); set1.Add("Andy"); set1.Add("Tim"); set1.Add("Steve"); set1.Add("Mark"); Console.WriteLine("Elements in HashSet1..."); foreach (string res in set1){ Console.WriteLine(res); } HashSet<string> set2 = new HashSet<string>(); set2.Add("Kevin"); set2.Add("Jacob"); set2.Add("Ryan"); set2.Add("Tom"); set2.Add("Andy"); set2.Add("Tim"); set2.Add("Steve"); set2.Add("Mark"); Console.WriteLine("Elements in HashSet2..."); foreach (string res in set2){ Console.WriteLine(res); } Console.WriteLine("Is HashSet1 equal to HashSet2? = "+set1.Equals(set2)); set2.ExceptWith(set1); // displaying elements in set2, which are not on set1 foreach(string i in set2){ Console.WriteLine(i); } } }
Output
This will produce the following output −
Elements in HashSet1... Jacob Ryan Tom Andy Tim Steve Mark Elements in HashSet2... Kevin Jacob Ryan Tom Andy Tim Steve Mark Is HashSet1 equal to HashSet2? = False Kevin
- Related Articles
- Remove all elements from a HashSet in C#
- Remove all elements from a HashSet in Java
- Remove all elements from the Collection in C#
- How to remove all elements from a Collection in another Collection
- Add all the elements from a collection to the HashSet in Java
- Remove all elements from a SortedList in C#
- Remove elements from a HashSet with conditions defined by the predicate in C#
- Create HashSet from another collection in C#
- Remove the specified element from a HashSet in C#
- Remove all elements from OrderedDictionary in C#
- Remove all the elements from an ArrayList that are in another Collection in Java
- Remove single element from a HashSet in Java
- Remove all elements from the ArrayList in C#
- Remove all elements from the SortedSet in C#
- Remove all elements from the Hashtable in C#

Advertisements