Search⌘ K
AI Features

Dictionary

Explore how to work with dictionary data structures in JavaScript. Understand key concepts such as handling duplicate keys, reasons for not using plain JavaScript objects, and practical implementations. Learn to add, lookup, and remove dictionary elements through interactive examples and exercises.

Introduction

Dictionary is a data structure that consists of key-value pairs. It's a data structure where a key is associated with a value. Hence, they are also called associative arrays. Dictionaries provide lookups on their keys. 

Let's go through a quick visualization:

Duplicates in Dictionary

We are not going to handle duplicates in our dictionary. Most implementations of associative arrays don't support duplicates as it is mostly thought of as a 1:1 relationship between a key and a value instead of a 1:N relationship. However, if you are in a situation where you need to associate duplicate values to a key, it's not that difficult to implement either.

...

For simplicity, we are going only to accept strings or integers as keys. Why not other types of keys? We can do it, but we have to then define a standard way to compare and equate objects. For now, we'll keep it simple as Javascript already provides a way to compare integers and strings.

We are also going to consider an integer and its string representation as the same key e.g. 45 and "45" will refer to the same key.

...