Data Structures Pt. 3 “Hash Tables”

Jared Matta
3 min readSep 7, 2020

If you are in the process of your job search and job interviews, I am sure you’ve come across the word Data Structures. What are they and how can we use them in our daily programming. I’ve started a blog series on Data Structures, touching on a different type each week. We will be using Javascript as the programming language as we dive into Data Structures. Last time we discussed Stacks, this time we will talk about hash tables or dictionaries, so let’s get started.

What are Data Structures? Data structures are all about runtime complexity, and there are two main things we need to know. Data structures are ways of organizing information with optimal ‘runtime complexity’ for adding and or removing records. Second, Javascript implements several data structures, but you will be asked about ‘inferior’ data structures, as well.

What is a hash table? Hash tables, hashes in Ruby, dictionaries in Python, maps in Java, unordered maps, every language has a different name for them, but they are relatively the same. In javascript objects are an example of hash tables. In an array, we have an index that’s numbered and a value, but storing more complex data in a database is a little difficult with arrays. So we have a hash table that allows us to set a key and a value for every entry. Having a key-value pair, allows extremely fast data fetching in our memory. Let’s create a hash table the data structure way without writing out a typical hash table.

Creating a hash table: Let’s start by writing a class called hash table because we don’t want to simply call a hash for data structure interview questions. We start by creating a constructor function that sets this data to a new array taking in a size. Next, we will write a private method that simply loops through our hash and gets the char value at the given input, and return it.

Writing our set method: This is a method that allows takes in a key and value and allows us to set the value for each. Let’s set an address variable to the -hash key from the private method above. If there is no data at this address leave it at an empty array. Else push the key and value data to this data address, and return it.

Writing our get method: With our get method, we simply want to return the value for the key we take in. We will set a const variable equal to the address at this data. If the currentBucket is true, loop through currentBucket and compare it to the key given, and return it.

Calling our Hash Table: Once we create a new instance of this object we can call our written methods on it. Starting with the empty array we can set key-value pairs via our .set method and passing in a key and a value. Similarly, we can access that key with our get method, assuming we have a key in our hash table that is related.

Wrapping it up: Look at that we created a hash table without actually writing it out. We are on our way to understanding algorithms and data structures to pass our technical interviews. Next, we will look at the linked list which happens in hash tables when a collision occurs. Stay tuned, and thank you for reading.

--

--

Jared Matta

Flatiron Graduate, Full-Stack developer on his job search. I love working with Javascript and React.