Collection Kit

Set

Hash-Based

A Set is a collection of unique elements that supports efficient membership tests and operations like union and intersection.

Import Statement
import { Set } from "collection-kit";

Key Features

  • Unique Elements: Does not allow duplicate values.
  • Dynamic Size: Can grow and shrink as elements are added or removed.

Common Operations

Add
Insert an element into the set.
Remove
Delete an element from the set.
Contains
Check if an element exists in the set.
Size
Return the number of unique elements in the set.

Example Code

import { Set } from "collection-kit";

const set = new Set();
set.add(10);
set.add(20);
set.add(10); // duplicate, won't be added

console.log("Size:", set.size());        // 2
console.log("Contains 20:", set.contains(20)); // true
set.remove(10);