Data Structures: Hash Tables

Dean
2 min readSep 6, 2020

A hash table is a key-value pair that is stored in memory in no particular order. You may have seen these in a variety of languages under different names. In JavaScript they are called “Objects”, in Python they are called “Dictionaries”, in Ruby they are call “Hashes” the concept is the same.

//this is a hash
{key: value}

As said before a Hash Table stores data by key value pairs in no particular order. If you can think of a excel sheet, a data structure such as an array would be stored consecutively in memory going from memory cell to memory cell, while a Hash Table will distribute the data into mostly available, mostly even, and mostly unique cells. (We’ll come back to this shortly)

Example of how an array would be stored in memory
Example of how a hash would be stored in memory

So why would you even use a Hash Tables? Because they are really fast when it comes to writing, reading, deleting, and searching data. As a matter of fact when it comes to making algorithms more speed efficient, 9 out of 10 times you’ll use a hash table to accomplish the task. Since we are using key-value pairs, as long as you know/have the key, all of the functions effectively have a O(1) time complexity.

That being said the trade offs of using a hash table is what it makes up for in time complexity, it lacks in space complexity, with the general average being O(n). Also addressing the “mostly’s” in the second paragraph, hash functions are oftentimes imperfect, that alongside the fact the memory is always limited, with enough data you can create ‘hash collisions’.

Hash Collisions, really simplified, is when two keys point to the same space in memory to store values. Usually the built in workaround for this is that the data initially stored in said memory cell will become a linked list pointing to the next value for the respective key. This can slow down time to performs lookups and creates/deletes from O(1) to O(n).

But in summary, hash tables are key value pairs that are generally useful to efficiently fetch data. They do have some downsides notably being space usage and the occasional collisions which slows down time to complete functions. Either way, they are an essential data structure that you will inevitably end up using through out your carreer.

--

--