Mastery Points
0

Sets with JS in dsa

Context & Logic

A Set is a collection of unique values. In JavaScript, the Set object allows you to store unique values of any type, whether primitive values or object references.

Example

const mySet = new Set([1, 2, 2, 3]);
console.log(mySet.size); // 3 (duplicate 2 ignored)
console.log(mySet.has(2)); // true (O(1))

Step-by-Step Logic

1

Add: Insert element if it doesn't already exist.

2

Has: Check if element exists during O(1) average time.

3

Delete: Remove element from the collection.

4

Iteration: Elements are iterated in insertion order.

Complexity Metrics

Time Efficiency

O(1) average for add/has/delete

Memory Footprint

O(n)