Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Introduction to Sets | Sets and Collection Utilities
C# Lists & Collections

bookIntroduction 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

Program.cs

copy
12345678910111213141516171819202122232425
using 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:

  1. Create a new HashSet<T> by specifying the type in angle brackets;
  2. Add elements using the Add method;
  3. Use Contains to check if an element is present;
  4. Remove elements with the Remove method.

This workflow allows you to manage a collection of unique items efficiently.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using 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>.

question mark

What is the main feature of a HashSet?

Select the correct answer

question mark

Which method checks if a value exists in a HashSet?

Select the correct answer

question-icon

Fill in the blanks to add elements to a HashSet<string>.

HashSet fruits = new HashSet(); fruits.("apple"); fruits.("banana");
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookIntroduction to Sets

Stryg for at vise menuen

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

Program.cs

copy
12345678910111213141516171819202122232425
using 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:

  1. Create a new HashSet<T> by specifying the type in angle brackets;
  2. Add elements using the Add method;
  3. Use Contains to check if an element is present;
  4. Remove elements with the Remove method.

This workflow allows you to manage a collection of unique items efficiently.

Program.cs

Program.cs

copy
123456789101112131415161718192021222324252627282930
using 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>.

question mark

What is the main feature of a HashSet?

Select the correct answer

question mark

Which method checks if a value exists in a HashSet?

Select the correct answer

question-icon

Fill in the blanks to add elements to a HashSet<string>.

HashSet fruits = new HashSet(); fruits.("apple"); fruits.("banana");
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
some-alt