Creating A Hash Table
Tags: javascript, hashtable, programming
Published: 2022-01-04 20:01:05

JavaScript includes Hash Tables which are a data structure that allow one to create a list of paired values. One is then able to retrieve a certain value by accessing the key for that value, which was previously placed into the table. Andrei Neagoie covered this in his Udemy course entitled “Master the Coding Interview: Data Structures + Algorithms.’

First a class named “HashTable” is created with a constructor having a parameter of the size with the data consisting of a new array with the same size as a parameter. 

A private method (which theoretically should not be reachable outside of its class) called  “_hash” is created with a parameter of the key. The hash is initiated with a value of 0. An iterating variable of “i” is created which is not allowed to exceed the length of the key. A variable called “hash” is then created containing The “charCodeAt()” method which returns the Unicode of the character at a specified index in a string.  The data length is then invoked and the hash returned. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a36zq1o1klgpahv6yc8y.png)

We next need a method to add keys and values to the Hash Table. A method called “set” is created with its parameters being the key and value. A variable “address” is created which equals the “_hash” method. If there is no current address then an empty array is instituted. If there is a current address then the new key/value pair is added. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d4179xic4q1c9nietuf.png)

The next step is to create a method which would allow one to retrieve information. One would do this by entering the key and then one could display either the key, the value, or both. A method called “get” is created.  In this method the variable “address” again equals the “_hash” method. A variable currentBucket is created equaling the address. As long as currentBucket exists, it is iterated over until the key equaling the desired key is located and in this case the value is returned (the key being at index 0 and the value at index 1). If the currentBucket does not exist then undefined is returned. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po7mrsgkavek9opsyjbq.png)

Finally a method called “keys” is created that will produce an array of all the keys. It has no parameters. A variable called “keysArray” is created which is an empty array. The data is the iterated over with the keys being pushed one by one into the empty array and the array containing the keys is returned. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jitdt8leff0w0mkfb1m.png)

The usage of this function entails using a variable to create a new instance of the class “HashTable” with the size as the argument.The methods within the function are then appended to the variable to obtain the desired results.  For the “set” method the key and value are the arguments and for the “get” method the key is the argument. The “keys” method has no arguments but don’t forget the (). An example is given below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d14ll28rfb0ht6jg8kja.png) 

Thank you for reading.