Mid From PDF Collections C# Collections

How do you remove duplicates from a collection using HashSet<T>?

Short answer: Add all elements from the collection to a HashSet<T>, which automatically removes duplicates. List<int> numbers = new List<int> { 1, 2, 2, 3, 3, 4 }; HashSet<int> uniqueNumbers = new HashSet<int>(numbers); Now, uniqueNumbers contains only unique values: {1, 2, 3, 4}.

Example code

Add all elements from the collection to a HashSet<T>, which automatically removes duplicates. List<int> numbers = new List<int> { 1, 2, 2, 3, 3, 4 };
HashSet<int> uniqueNumbers = new HashSet<int>(numbers); Now, uniqueNumbers contains only unique values: {1, 2, 3, 4}.

Real-world example (ShopNest)

When applying a coupon, ShopNest keeps used coupon codes in a HashSet<string> so “already used?” checks stay fast and unique.

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details