How to remove elements from the HashSet?
In HashSet, you are allowed to remove elements from the HashSet. HashSet<T> class provides three different methods to remove elements and the methods are:
- Remove(T): This method is used to remove the specified element from a HashSet object.
- RemoveWhere(Predicate): This method is used to remove all elements that match the conditions defined by the specified predicate from a HashSet collection.
- Clear: This method is used to remove all elements from a HashSet object.
Example 1:
C#
// C# program to illustrate how to // remove elements of HashSet using System; using System.Collections.Generic; class GFG { // Main Method static public void Main() { // Creating HashSet // Using HashSet class HashSet< string > myhash = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash.Add( "C" ); myhash.Add( "C++" ); myhash.Add( "C#" ); myhash.Add( "Java" ); myhash.Add( "Ruby" ); // Before using Remove method Console.WriteLine( "Total number of elements present (Before Removal)" + " in myhash: {0}" , myhash.Count); // Remove element from HashSet // Using Remove method myhash.Remove( "Ruby" ); // After using Remove method Console.WriteLine( "Total number of elements present (After Removal)" + " in myhash: {0}" , myhash.Count); // Remove all elements from HashSet // Using Clear method myhash.Clear(); Console.WriteLine( "Total number of elements present" + " in myhash:{0}" , myhash.Count); } } |
Total number of elements present in myhash: 5 Total number of elements present in myhash: 4 Total number of elements present in myhash:0
Set Operations
HashSet class also provides some methods that are used to perform different operations on sets and the methods are:
- UnionWith(IEnumerable): This method is used to modify the current HashSet object to contain all elements that are present in itself, the specified collection, or both.
Example:
C#
// C# program to illustrate set operations using System; using System.Collections.Generic; class GFG { static public void Main() { // Creating HashSet // Using HashSet class HashSet< string > myhash1 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash1.Add( "C" ); myhash1.Add( "C++" ); myhash1.Add( "C#" ); myhash1.Add( "Java" ); myhash1.Add( "Ruby" ); // Creating another HashSet // Using HashSet class HashSet< string > myhash2 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash2.Add( "PHP" ); myhash2.Add( "C++" ); myhash2.Add( "Perl" ); myhash2.Add( "Java" ); // Using UnionWith method myhash1.UnionWith(myhash2); foreach ( var ele in myhash1) { Console.WriteLine(ele); } } } |
C C++ C# Java Ruby PHP Perl
- IntersectWith(IEnumerable): This method is used to modify the current HashSet object to contain only elements that are present in that object and in the specified collection.
Example:
C#
// C# program to illustrate set operations using System; using System.Collections.Generic; class GFG { // Main Method static public void Main() { // Creating HashSet // Using HashSet class HashSet< string > myhash1 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash1.Add( "C" ); myhash1.Add( "C++" ); myhash1.Add( "C#" ); myhash1.Add( "Java" ); myhash1.Add( "Ruby" ); // Creating another HashSet // Using HashSet class HashSet< string > myhash2 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash2.Add( "PHP" ); myhash2.Add( "C++" ); myhash2.Add( "Perl" ); myhash2.Add( "Java" ); // Using IntersectWith method myhash1.IntersectWith(myhash2); foreach ( var ele in myhash1) { Console.WriteLine(ele); } } } |
C++ Java
- ExceptWith(IEnumerable): This method is used to remove all elements in the specified collection from the current HashSet object.
Example:
C#
// C# program to illustrate set operations using System; using System.Collections.Generic; class GFG { // Main Method static public void Main() { // Creating HashSet // Using HashSet class HashSet< string > myhash1 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash1.Add( "C" ); myhash1.Add( "C++" ); myhash1.Add( "C#" ); myhash1.Add( "Java" ); myhash1.Add( "Ruby" ); // Creating another HashSet // Using HashSet class HashSet< string > myhash2 = new HashSet< string >(); // Add the elements in HashSet // Using Add method myhash2.Add( "PHP" ); myhash2.Add( "C++" ); myhash2.Add( "Perl" ); myhash2.Add( "Java" ); // Using ExceptWith method myhash1.ExceptWith(myhash2); foreach ( var ele in myhash1) { Console.WriteLine(ele); } } } |
C C# Ruby
HashSet in C# with Examples
In C#, HashSet is an unordered collection of unique elements. This collection is introduced in .NET 3.5. It supports the implementation of sets and uses the hash table for storage. This collection is of the generic type collection and it is defined under System.Collections.Generic namespace. It is generally used when we want to prevent duplicate elements from being placed in the collection. The performance of the HashSet is much better in comparison to the list.
Important Points Related To HashSet in C#
- The HashSet class implements the ICollection, IEnumerable, IReadOnlyCollection, ISet, IEnumerable, IDeserializationCallback, and ISerializable interfaces.
- In HashSet, the order of the element is not defined. You cannot sort the elements of HashSet.
- In HashSet, the elements must be unique.
- In HashSet, duplicate elements are not allowed.
- It provides many mathematical set operations, such as intersection, union, and difference.
- The capacity of a HashSet is the number of elements it can hold.
- A HashSet is a dynamic collection means the size of the HashSet is automatically increased when the new elements are added.
- In HashSet, you can only store the same type of elements.