0
0
C Sharp (C#)programming~15 mins

HashSet for unique elements in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
HashSet for unique elements
📖 Scenario: You are organizing a guest list for a party. Some guests accidentally appear more than once on the list. You want to make sure each guest is invited only once.
🎯 Goal: Build a program that uses a HashSet to store unique guest names and then display the list of unique guests.
📋 What You'll Learn
Create a HashSet<string> to hold guest names
Add guest names to the HashSet, including duplicates
Use a foreach loop to iterate over the HashSet
Print each unique guest name
💡 Why This Matters
🌍 Real World
Removing duplicates from lists is common when managing contacts, emails, or any collection where uniqueness matters.
💼 Career
Understanding HashSet helps in writing efficient code for data processing, filtering, and ensuring data integrity in software development.
Progress0 / 4 steps
1
Create the initial guest list
Create a List<string> called guestList with these exact names: "Alice", "Bob", "Alice", "Charlie", "Bob".
C Sharp (C#)
Need a hint?

Use new List<string> { ... } to create the list with the exact names.

2
Create a HashSet for unique guests
Create a HashSet<string> called uniqueGuests and initialize it with the guestList.
C Sharp (C#)
Need a hint?

Use the HashSet constructor that takes a collection to remove duplicates.

3
Iterate over the unique guests
Use a foreach loop with the variable guest to iterate over uniqueGuests.
C Sharp (C#)
Need a hint?

Write foreach (string guest in uniqueGuests) to loop through the unique names.

4
Print the unique guest names
Inside the foreach loop, write Console.WriteLine(guest); to print each unique guest name.
C Sharp (C#)
Need a hint?

Use Console.WriteLine(guest); inside the loop to print each name.