What is Set in TypeScript?
1. What Set is
Set<T>is a collection of values of typeT, where each value occurs only once.
Example:
javascript
const numbers = new Set<number>([1, 2, 3, 3, 2]);
console.log(numbers); // Set(3) { 1, 2, 3 }All duplicates were removed automatically. The order of elements is kept in insertion order.
2. Declaring a Set with a type
javascript
const names: Set<string> = new Set(["Tim", "Max", "Oleh"]);Each element will be strictly
string. Trying to add a number causes an error:
javascript
names.add("Bob"); // OK
names.add(123); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.3. Main Set methods
| Method | What it does | Example |
|---|---|---|
.add(value) | Adds an element | set.add(5) |
.delete(value) | Removes an element | set.delete(2) |
.has(value) | Checks presence | set.has(3) -> true |
.clear() | Removes all elements | set.clear() |
.size | Number of elements | set.size -> 3 |
.forEach(fn) | Iterates elements | set.forEach(v => console.log(v)) |
Usage example:
javascript
const ids = new Set<number>();
ids.add(10);
ids.add(20);
ids.add(10); // duplicate - ignored
console.log(ids.has(10)); // true
console.log(ids.size); // 24. Iterating a Set
Set is an iterable structure, so you can use:
javascript
for (const value of ids) {
console.log(value);
}
ids.forEach(v => console.log(v));or turn it into an array:
javascript
const arr = [...ids]; // [10, 20]5. Differences between Set and an array
| Property | Array | Set |
|---|---|---|
| Duplicates | allowed | not allowed |
| Presence check | slow (arr.includes()) | fast (set.has()) |
| Element order | preserved | preserved |
| Access by index | yes (arr[0]) | no |
| Adding | push() | add() |
| Removing | splice() | delete() |
| Size | arr.length | set.size |
| Type | T[] | Set<T> |
Comparison example
javascript
const arr = [1, 2, 2, 3];
const set = new Set(arr);
console.log(arr.length); // 4
console.log(set.size); // 3
Setautomatically removed the duplicates.
6. Converting between Array and Set
From an array to a Set
javascript
const arr = [1, 2, 3, 3];
const unique = new Set(arr); // Set(3) {1, 2, 3}From a Set to an array
javascript
const arr2 = Array.from(unique); // [1, 2, 3]or
javascript
const arr3 = [...unique]; // [1, 2, 3]7. Typing a Set with objects
You can store more than just primitives:
javascript
interface User {
id: number;
name: string;
}
const users = new Set<User>();
users.add({ id: 1, name: "Tim" });
users.add({ id: 1, name: "Tim" }); // both remain, because they are different references
Setchecks uniqueness by reference, not by an object's content. That is,{id:1,name:"Tim"}and{id:1,name:"Tim"}are different elements.
8. A practical usage example
Removing duplicates from an array
javascript
const numbers = [1, 2, 2, 3, 4, 3];
const unique = [...new Set(numbers)];
console.log(unique); // [1, 2, 3, 4]Counting unique values
javascript
function countUnique<T>(arr: T[]): number {
return new Set(arr).size;
}
console.log(countUnique(["a", "b", "a", "c"])); // 3Summary
Setis a collection of unique values, with fast lookup and iteration.The main differences from an array:
- does not store duplicates;
- has no indexes;
- checks for the presence of elements faster;
- stores values in insertion order.
The basic type:
javascriptconst set: Set<number> = new Set([1, 2, 3]);
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.