Creating the HashTable class
Our hash table implementation will be located in the src/08-dictionary-hash/hash-table.js
file. We begin by defining the HashTable
class:
class HashTable {
#table = [];
}
This initial step simply initializes the private #table
array, which will serve as the underlying storage for our key-value pairs.
Next, we will equip our HashTable
class with three essential methods:
put(key, value)
: this method either adds a new key-value pair to the hash table or updates the value associated with an existing key.remove(key)
: this method removes the value and its corresponding key from the hash table based on the provided key.get(key)
: this method retrieves the value associated with a specific key from the hash table.
To enable the functionality of these methods, we also need to create a crucial component: the hash function. This function will play a vital role in determining the storage location of each key-value pair within the hash table, making it a cornerstone...