In this post, we will explore the HashSet<T>.ExceptWith(IEnumerable<T>) function in C# along with its syntax and illustrations.
What is the HashSet__PRESERVE_6__.ExceptWith(IEnumerable__PRESERVE_7__) Method?
The ExceptWith function is commonly employed to alter the HashSet<T> instance by eliminating all items found in another collection. It functions akin to the mathematical set subtraction operation. The time complexity associated with this operation is O(n), where n represents the overall count of elements contained within the other specified collection. Upon execution, this function will retain only those elements unique to the original set after the subtraction process.
For instance, consider two HashSets of identical type, such as both being integers. One is denoted as HashSet1 and the other as HashSet2. Subsequently, the ExceptWith method is applied to HashSet1, with HashSet2 provided as the argument. This action results in the removal of elements present in both HashSets from HashSet1.
Syntax:
It has the following syntax:
public void ExceptWith(IEnumerable<T> other);
Here, the second parameter will denote the collection containing elements to be deleted from the existing HashSet<T>. This function does not return any value since it is void, yet it modifies the original HashSet instance. It stands out as the most optimal approach for executing a set difference operation.
Example:
Let's consider a program to illustrate how the ExceptWith method functions with hash sets in C#.
using System;
using System.Collections.Generic;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// first HashSet with some initial items
HashSet<string> hashSet1 = new HashSet<string> { "Apple", "Banana", "Milk", "Bread" };
// second HashSet with some initial items
HashSet<string> hashSet2 = new HashSet<string> { "Milk", "Bread", "Chips" };
// removing the items of second hashset from the first hashset
hashSet1.ExceptWith(hashSet2);
// Display the updated first hashset
Console.WriteLine("Items in the hashSet1 after removing items from hashSet2:");
foreach (string item in hashSet1)
{
Console.WriteLine(item);
}
}
}
}
Output:
The <style> CSS code snippet displays a diagram with styling properties like background color, border radius, padding, margin, and text alignment. Within the diagram, there is an icon with a specific font size and margin, along with text styled in a particular color and font size. This setup is essential for creating visually appealing and structured content within a web page.
Explanation:
This code snippet demonstrates the implementation of the ExceptWith function with hashsets. In this instance, two hashsets, referred to as hashSet1 and hashSet2, are initialized with specific elements. Subsequently, the ExceptWith function is invoked on hashSet1 with hashSet2 as the parameter. This operation eliminates the shared elements between the two hashsets from hashSet1. Finally, a foreach loop is utilized to display the modified hashSet1.
Example 2:
Let's consider a C# code example to showcase the functionality of the ExceptWith method through a brief real-world scenario.
using System;
using System.Collections.Generic;
namespace HashSetExample
{
class Program
{
static void Main(string[] args)
{
// Suppose we have a list of students who attended a science fair
HashSet<string> allStudents = new HashSet<string>
{
"Ramu", "Laxman", "sita", "urmila", "ravan", "Sophia", "Michael"
};
// Now, let's say we have a list of students who won prizes at the science fair
HashSet<string> prizeWinners = new HashSet<string>
{
"Ramu", "sita", "Sophia"
};
Console.WriteLine("List of all students who attended the science fair:");
foreach (string student in allStudents)
{
Console.WriteLine(student);
}
Console.WriteLine("\nList of students who won prizes at the science fair:");
foreach (string winner in prizeWinners)
{
Console.WriteLine(winner);
}
// Now, let's find out who attended the science fair but didn't win any prize
allStudents.ExceptWith(prizeWinners);
Console.WriteLine("\nList of students who attended the science fair but didn't win any prize:");
foreach (string attendee in allStudents)
{
Console.WriteLine(attendee);
}
}
}
}
Output:
The styling for the placeholder element is defined in the following CSS code block:
.placeholder-diagram {
background: linear-gradient(135deg, #374151 0%, #1f2937 100%);
border-radius: 12px;
padding: 40px;
margin: 20px 0;
text-align: center;
}
.placeholder-diagram .placeholder-icon {
font-size: 3rem;
margin-bottom: 10px;
}
.placeholder-diagram .placeholder-text {
color: #9ca3af;
font-size: 1rem;
}
Explanation:
In this illustration, we present a practical scenario involving the utilization of an ExceptWith method. The code snippet involves two hashsets: allStudents and prizeWinners. The allStudents hashset encompasses the names of all participants in a science fair, while the prizeWinners hashset includes the names of the students who emerged victorious in the science fair. To identify the students who participated in the science fair but didn't secure any prizes, we can employ the ExceptWith method. This method effectively provides us with the list of students who attended the science fair but did not achieve any awards.
Example 3:
Let's consider a different C# program where the List is passed as another argument to the ExceptWith method.
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
HashSet<string> shoppingCart = new HashSet<string> { "Apple", "Banana", "Milk", "Bread" };
// List representing items to be removed from the shopping cart
List<string> itemsToRemove = new List<string> { "Milk", "Bread", "Chips" };
shoppingCart.ExceptWith(itemsToRemove);
// Display the updated shopping cart
Console.WriteLine("Items in the shopping cart after removing unwanted items:");
foreach (string item in shoppingCart)
{
Console.WriteLine(item);
}
}
}
Output:
The <style> component includes a diagram with a stylish background created using a linear gradient. The diagram is enclosed within a container with rounded corners, adding a touch of sophistication to the overall design. The central alignment of elements within the container enhances the visual appeal of the diagram. Additionally, the diagram features an icon that is prominently displayed and accompanied by descriptive text in a subdued color, ensuring a balanced and aesthetically pleasing presentation. </style>
Explanation:
In this code snippet, the initial shoppingCart object is created to simulate a shopping cart, followed by the instantiation of a list to specify the items to be eliminated from the current cart. The ExceptWith method is employed as usual to eliminate the shared elements. Subsequently, a loop iterates through to exhibit the items in the revised shopping cart.