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. Search: Map
  3. JavaScript
  4. Map, Set, WeakMap & WeakSet
  5. Question
All 10 Map, Set, WeakMap & WeakSet questions
More in Map, Set, WeakMap & WeakSet
How does a Map differ from a plain object for storing key-value pairs? How do you iterate over a Map's entries? What is a WeakMap and why can't you iterate over its keys? When would you choose a WeakSet over a Set? How do you convert between a Map and a plain object? How would you use a Map to implement an LRU (Least Recently Used) cache? How do WeakRef and FinalizationRegistry extend the weak-reference capabilities beyond WeakMap/WeakSet? Why can't you use WeakMap for caching results keyed by primitive values like strings or numbers? How would you group an array of objects by a property using Map, and how does the newer Object.groupBy() compare?
Advertisement
Back to results for “Map”
JavaScript › Map, Set, WeakMap & WeakSet

How does a Set differ from an array?

Beginner

Answer

A Set only stores unique values — adding a duplicate is a no-op — and lookups with .has() are much faster (O(1) on average) than an array's .includes() (O(n)).
const set = new Set([1, 2, 2, 3]);
console.log(set.size);      // 3
console.log(set.has(2));    // true
Real-world example Tracking which user IDs have already been processed to avoid duplicate work.

Common follow-ups: How do you convert a Set back into an array?

Arrays & Array Methods
Previous question How does a Map differ from a plain object for storing key-value pairs? Next question How do you iterate over a Map's entries?
All 10 Map, Set, WeakMap & WeakSet 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.