Using Hash Tables

Daniel C Reyes
3 min readFeb 28, 2021

--

Beginners guide to Hash Tables

While I continue my journey working on algorithms in preparation for what the next technical interview will throw at me next, I came across a problem that eventually led me into the world of Hash Tables. As any good developer would do, I took a deeper look into what hash tables are and how they can be beneficial to writing efficient code.

What are Hash Tables?

Hash tables are data structures for storing key/value pairs. Hash tables contain slots called buckets for keeping items. The number of these buckets or slots represents the length of our hash table. For example, if we have a hash table of length 1000, it means it has 1000 slots or buckets to keep 1000 items. Hash tables use a hash function to efficiently map keys to values, for efficiently search for an item, insert an item, and remove an item.

Hashing Functions

As mentioned above hash tables use hashing functions to store a value in our hash table. The number that determines the index will come from hashing our label using a hashing function. A hashing function takes two inputs, any data type, and a number. The number is the length of our hash table as the function can only return numbers as long as the length of the array. a hashing function tells us where to find an element in the array by inputting a key. Basically, the function will map a data type to a number. Ideally, our hashing function will output unique index numbers for each key.

Hash Tables in Action

The way a Hash table work is using Key, Value lookup

1. You take input keys run it through a hash function where it will map those input keys into numbers that correspond to a number of indexes in an array.

2. Run these inputs through a hash function. There are many different ways you can code a hash function, but essentially the hash function should take in an input, a data type, and then converts them into an integer, finally remaps that integer into an index in an array.

3. We now have associative values. Where we can now search, insert, and delete our data by using the Array index.

Why hash tables?

When I was diving deeper into hash tables I found out that they use an array to store items. I quickly asked my self “What’s the point of using hash tables if they use Arrays for storing, why not just use a simple array?” Down to the core of things, yes, but also remember data structures like hash tables are methods that make your code run more efficiently! Hash tables are one of the more popular methods use because of how efficient they are.

Hash tables are the favored data structure because on average they provide a time complexity O(1)for insertion, deletion, and search, which is pretty fast compared to all of the other data structure methods. Hash tables don’t need to look through every bucket or slot for a value because it is associated with a key. All the hash table will need is the key to directly find its value.

--

--

Daniel C Reyes
Daniel C Reyes

Written by Daniel C Reyes

Full-Stack Software Engineer / New York City / www.danielcreyes.dev

No responses yet