W3Teacher
  • Explore
    Browse by field
    Artificial Intelligence Accounting & Finance Information Technology
    Show all fields
  • Interview Q&A
    • All Interview Questions
    • Beginner
    • Intermediate
    • Advanced
  • Practice
    • MCQs
    • Quizzes
    • Mock Interviews
  • About
  • Sign in
  • Get Started
  1. Home
  2. C#
  3. Collections
  4. Question
All 16 Collections questions
More in Collections
When should you use a Dictionary<TKey,TValue>? What is the difference between List<T> and an array (T[]) in terms of resizing? What is the difference between Dictionary<TKey, TValue> and List<T> in terms of lookup performance? What is the difference between IEnumerable, ICollection and IList? When would you choose a HashSet<T> over a List<T>, and what guarantee does it provide? How does Queue<T> differ from Stack<T> in terms of the order items are removed? What is the difference between IEnumerable<T>, ICollection<T>, and IList<T> in the collection interface hierarchy? When would you reach for a ConcurrentDictionary or an immutable collection? How do you avoid multiple enumeration of an IEnumerable? How does Dictionary<TKey,TValue> handle hash collisions internally, and why does a poor GetHashCode() implementation degrade its performance?
Advertisement
C# › Collections

What does HashSet<T> provide and when do you use it?

Intermediate

Answer

A set of unique elements with fast Contains/Add and set operations. Adding a duplicate is a no-op.
var seen = new HashSet<int>();
if (!seen.Add(id)) Console.WriteLine("duplicate");
Real-world example De-duplicating ids or checking membership in a loop without O(n) scans.
Previous question What is the difference between IEnumerable, ICollection and IList? Next question When would you choose a HashSet<T> over a List<T>, and what guarantee does it provide?
All 16 Collections questions
W3Teacher

Learn technologies. Ace technical interviews.


    Learn
    • Technologies
    • Tutorials
    • Interview Questions
    • MCQs
    Practice
    • Quizzes
    • Mock Interviews
    • Bookmarks
    • Dashboard
    Get the latest questions & tips

    Fresh interview questions and practice tips, straight to your inbox.

    Company
    • About
    • Contact us
    • Privacy Policy
    • Terms & Conditions

    © 2026 W3Teacher. All rights reserved.