Dictionary Data Structure in Javascript

Web DevelopmentFront End TechnologyJavascript

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. Note that a dictionary is also known as a map.

The dictionary problem is a classic computer science problem: the task of designing a data structure that maintains a set of data during 'search', 'delete', and 'insert' operations. There are many different types of implementations of dictionaries.

  •  Hash Table implementation
  •  Tree-Based Implementation (Self-balancing and Unbalanced trees)
  •  List based implementation

When to use a Dictionary

Dictionaries are not a silver bullet and should not be used at every chance you get. They are useful in many scenarios, but you need to keep the following points in mind before deciding to use a dictionary to solve a problem.

  • Inserts are generally slow, reads are faster than trees.
  • Use these for fast lookups, for example, to cache data, index databases, symbol tables, etc.
  • When the order of elements doesn't matter.
  • When all element keys are unique.

Methods we'll implement

Dictionaries generally have a well-defined APIs. We're going to implement a very basic dictionary API as defined below −

  • get(): Gets the element with the input key
  • put(): Puts the key-value pair in the dictionary
  • hasKey(): Checks if the key is present in the dictionary
  • delete(): Removes the given key from the dictionary
  • clear(): Removes all key-value pairs from the dictionary
  • keys(): Returns all keys as an array
  • values(): Returns all values as an array
raja
Published on 27-Jun-2018 16:48:28
Advertisements