Bird
0
0

Why does a HashSet<T> provide faster lookup than a List<T> for checking if an element exists?

hard🧠 Conceptual Q10 of 15
C Sharp (C#) - Collections
Why does a HashSet<T> provide faster lookup than a List<T> for checking if an element exists?
AHashSet stores elements in sorted order for binary search.
BList caches elements for faster repeated lookups.
CList uses a hash table internally, making it slower.
DHashSet uses a hash table for O(1) average lookup time.
Step-by-Step Solution
Solution:
  1. Step 1: Understand HashSet internal structure

    HashSet uses a hash table which allows average constant time O(1) lookup.
  2. Step 2: Compare with List lookup

    List requires scanning elements linearly, O(n) time, slower for large collections.
  3. Final Answer:

    HashSet uses a hash table for O(1) average lookup time. -> Option D
  4. Quick Check:

    HashSet lookup speed = O(1) via hash table [OK]
Quick Trick: HashSet lookup is fast due to hashing, unlike List [OK]
Common Mistakes:
MISTAKES
  • Thinking HashSet is sorted
  • Believing List uses hashing internally
  • Assuming List caches lookups

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes