Introduction to Sets
A set is a collection type that stores unique elements only—no duplicates are allowed. In C#, the most common implementation is HashSet<T>, where T is the type of elements you want to store. This makes sets especially useful when you need to eliminate duplicates or quickly check if an item already exists.
To use a HashSet<T>, you declare it by specifying the type of elements in angle brackets. For example, to store integers, you write HashSet<int>. You can add elements using the Add method, and if you try to add a duplicate, the set will simply ignore it. This built-in duplicate prevention is what makes sets different from lists or arrays, which allow repeated values.
Program.cs
12345678910111213141516171819202122232425using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> numbers = new HashSet<int>(); numbers.Add(10); numbers.Add(20); numbers.Add(30); numbers.Add(10); // Duplicate, will be ignored Console.WriteLine("Numbers in the set:"); foreach (int number in numbers) { Console.WriteLine(number); } } } }
When you add elements to a HashSet<T>, each value is checked for uniqueness. If the value already exists, it is not added again. To check if a value exists, use the Contains method. To remove a value, use the Remove method. Here is a step-by-step breakdown:
- Create a new
HashSet<T>by specifying the type in angle brackets; - Add elements using the
Addmethod; - Use
Containsto check if an element is present; - Remove elements with the
Removemethod.
This workflow allows you to manage a collection of unique items efficiently.
Program.cs
123456789101112131415161718192021222324252627282930using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 }; HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 }; setA.UnionWith(setB); Console.WriteLine("After UnionWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } setA = new HashSet<int> { 1, 2, 3, 4 }; setA.IntersectWith(setB); Console.WriteLine("After IntersectWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } } } }
1. What is the main feature of a HashSet?
2. Which method checks if a value exists in a HashSet?
3. Fill in the blanks to add elements to a HashSet<string>.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you give an example of how to use HashSet in C#?
What are some real-world scenarios where HashSet is useful?
How does HashSet compare to other collection types like List or Dictionary?
Génial!
Completion taux amélioré à 4.76
Introduction to Sets
Glissez pour afficher le menu
A set is a collection type that stores unique elements only—no duplicates are allowed. In C#, the most common implementation is HashSet<T>, where T is the type of elements you want to store. This makes sets especially useful when you need to eliminate duplicates or quickly check if an item already exists.
To use a HashSet<T>, you declare it by specifying the type of elements in angle brackets. For example, to store integers, you write HashSet<int>. You can add elements using the Add method, and if you try to add a duplicate, the set will simply ignore it. This built-in duplicate prevention is what makes sets different from lists or arrays, which allow repeated values.
Program.cs
12345678910111213141516171819202122232425using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> numbers = new HashSet<int>(); numbers.Add(10); numbers.Add(20); numbers.Add(30); numbers.Add(10); // Duplicate, will be ignored Console.WriteLine("Numbers in the set:"); foreach (int number in numbers) { Console.WriteLine(number); } } } }
When you add elements to a HashSet<T>, each value is checked for uniqueness. If the value already exists, it is not added again. To check if a value exists, use the Contains method. To remove a value, use the Remove method. Here is a step-by-step breakdown:
- Create a new
HashSet<T>by specifying the type in angle brackets; - Add elements using the
Addmethod; - Use
Containsto check if an element is present; - Remove elements with the
Removemethod.
This workflow allows you to manage a collection of unique items efficiently.
Program.cs
123456789101112131415161718192021222324252627282930using System; using System.Collections.Generic; namespace ConsoleApp { public class Program { public static void Main(string[] args) { HashSet<int> setA = new HashSet<int> { 1, 2, 3, 4 }; HashSet<int> setB = new HashSet<int> { 3, 4, 5, 6 }; setA.UnionWith(setB); Console.WriteLine("After UnionWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } setA = new HashSet<int> { 1, 2, 3, 4 }; setA.IntersectWith(setB); Console.WriteLine("After IntersectWith, setA contains:"); foreach (int n in setA) { Console.WriteLine(n); } } } }
1. What is the main feature of a HashSet?
2. Which method checks if a value exists in a HashSet?
3. Fill in the blanks to add elements to a HashSet<string>.
Merci pour vos commentaires !