Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

typescript

What is the set.delete() method in TypeScript?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

A set is a data structure to store unique values that must not have duplicates. With the delete() method, we can delete an entry from a set.

Syntax

set.delete(value)
The syntax for the set.delete() method in TypeScript

Parameters

set: This is the set from which we want to delete an entry.

value: This is the value we want to delete from the set.

Return value

If the entry is found and successfully deleted, then true is returned. Otherwise, false is returned.

// create some sets
let names = new Set<string>(["Theodore", "David", "John", "Janme"])
let evenNumbers = new Set<number>([2, 4, 6, 8, 10, 12])
let booleanValues = new Set<boolean>([true, false])
let countries = new Set<string>(["Nigeria", "Brazil", "Ghana", "Egypt", "Germany"])
// check if they contain some values
console.log(names.delete("Theodore")) // true
console.log(evenNumbers.delete(5)) // false
console.log(booleanValues.delete(true)) // true
console.log(countries.delete("Tokyo")) // false
// log out the sets
console.log(names)
console.log(evenNumbers)
console.log(booleanValues)
console.log(countries)
The set.delete method in TypeScript

Explanation

  • Lines 2–5: We create some sets.
  • Lines 8–11: We use the delete() method to delete some entries of the sets we created. Then, we log out the returned result to the console.
  • Lines 14–17: We log out the sets to the console after the deletion of some entries.

RELATED TAGS

typescript

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring