Skip to main content

What is Set in TypeScript?

1. What Set is

Set<T> is a collection of values of type T, 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

MethodWhat it doesExample
.add(value)Adds an elementset.add(5)
.delete(value)Removes an elementset.delete(2)
.has(value)Checks presenceset.has(3) -> true
.clear()Removes all elementsset.clear()
.sizeNumber of elementsset.size -> 3
.forEach(fn)Iterates elementsset.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); // 2

4. 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

PropertyArraySet
Duplicatesallowednot allowed
Presence checkslow (arr.includes())fast (set.has())
Element orderpreservedpreserved
Access by indexyes (arr[0])no
Addingpush()add()
Removingsplice()delete()
Sizearr.lengthset.size
TypeT[]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

Set automatically 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

Set checks 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"])); // 3

Summary

Set is 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:

javascript
const set: Set<number> = new Set([1, 2, 3]);

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.