Understanding Hash Tables

Software engineer with experienced in fullstack development and project management.
Studied CS @ Auburn University.
Search for a command to run...

Software engineer with experienced in fullstack development and project management.
Studied CS @ Auburn University.
No comments yet. Be the first to comment.
Reflecting on 2020 was eye-opening for me. While I never had a shortage of goals I wanted to accomplish, I struggled with organizing those goals into focused efforts. So this year I committed to a different approach, and created a 3-step process to k...

2020 didn't quite go according to plan, but it still had plenty of silver linings. I spent some time this week reflecting on the last 365 days and even though my 2020 resolutions were a bit unrealistic, I had a more productive year than I thought. �...

I recently led a corporate website redesign, which involved a move from Drupal 7 to Drupal 8. I wanted to share my process for tackling this kind of CMS-based project. Disclaimer: The process outlined below is a guide. Feel free to adapt it to fit ...

It Doesn't Have To Be Crazy At Work is a manifesto written by Jason Fried & David Heinemeier Hansson, the creators of the productivity platform Basecamp. It describes the fundamental (and sometimes unconventional) values that define Basecamp's unique...

Hash tables are an excellent data structure to use when you're working with 2 related types of data. This relationship is established by using key-value pairs, where one data type is assigned as the key, and the other data type is assigned as the value.
Hash tables are ideal when you need to insert, search, and delete objects from your dataset often. In fact, hash tables perform these operations so efficiently that they have an average time complexity of O(1)—that's right, constant time!
Hash tables have many real-world applications, but here are some practical examples.
Hash tables go by many names: dictionary, associative array, map, hash map, hash, etc. They're built into most programming languages but it's important to understand how they work under the hood, even if you never have to implement one from scratch.
Hash tables are powered by 2 components: a hash function and an array table. Here's a breakdown of how these components process and store data.
Using the hash table structure that's built into a programming language means that you don't have to worry about:
When you need to look up a value, the hash table will perform a direct lookup of the key that corresponds to that value. There is no need to sort or iterate through a list to search for the data!
Tip: If you're interested in building your own hash functions and tables, check out William Fiset's Easy to Advanced Data Structures course. You'll learn how to choose the right hash function for your use case, as well as different techniques for managing collisions.
While hash tables provide many benefits, they're not suited for every situation. Here are a few important things to keep in mind.
This one's for my fellow musicians! Let's set up a music catalogue that stores a piece of music as the key, and a composer as the value.
As part of my 2021 goals, I implemented the example below in both Python and JavaScript.
#############################################
#create a hash table for the music catalogue
music_catalogue = dict()
#add a piece of music to the catalogue
music_catalogue["Symphony No. 5"] = "Beethoven"
#function to search the catalogue for a composer
def composer_search(piece):
#retrieve the name of the composer if s/he is in the catalogue
#this step isn't necessary but it's easier to visualize
composer = music_catalogue.get(piece)
#if the composer is in the catalogue, print his/her name
if (composer):
print(piece + " was written by " + composer)
else:
print("Error: composer not found!")
Now let's try out this catalogue by calling the composer_search function.
########################################################
#someone wants to find out who composed "Symphony No. 5"
piece = "Symphony No. 5"
#check the registry for "Symphony No. 5"
composer_search(piece)
The console output will read: Symphony No. 5 was written by Beethoven.
/////////////////////////////////////////////
//create a hash table for the music catalogue
musicCatalogue = {};
//add a piece of music to the catalogue
musicCatalogue["Symphony No. 5"] = "Beethoven";
//function to search the hash table for a composer
function composerSearch(piece) {
//retrieve the name of the composer if s/he is in the catalogue
//this step isn't necessary but it's easier to visualize
const composer = musicCatalogue[piece];
//if the composer is in the catalogue, print his/her name
if (composer) {
console.log(`${piece} was written by ${composer}`);
} else {
console.log(`Error: composer not found!`);
}
}
Now let's try out this catalogue by calling the composerSearch function.
/////////////////////////////////////////////////////////
//someone wants to find out who composed "Canon in D"
const piece = "Canon in D"
//check the registry for "Canon in D"
composerSearch(piece);
The console output will read: Error: composer not found!
Here are some great resources to dive deeper into hash tables and hash functions.
Books:
Courses:
Blogs:
Happy Coding! 👩🏽💻