Data Structures — Set & Map

--

Set

A set is a dynamic data structure with NO duplicates. A set is unordered, and has no index. Sets can store any types of values (primitive or objects).

Methods for a Set: .has(), .add(), .delete(), .size(), .cleare()

Used for: easily removes all duplicates from an array

const myArray = ['Bill','Bob','Bob','Ben']

const mySet = new Set(myArray)

let uniqueArray = mySet // ['Bill','Bob',Ben']

Map Constructor

Key value pairs with distinct keys. Remembers the original insertion order of the keys

Methods for a Map: .has(),.set(),.delete(),.size(),.clear()

Why we use Map() instead of a Javascript Object? Objects only support one key object, but maps support multiple key objects

const a = {}

const b = {}

const myMap = new Map([ [a, 'a'], [ b, 'b']) //{{} => 'a', {} => 'b'}

https://www.youtube.com/watch?v=hLgUTM3FOII

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response