Understanding Hash Maps, Hash Tables, and Hash Sets in Python

Abdullah Saeed
4 min readJul 27, 2023

--

Personally, I found hash maps, hash tables, and hashsets difficult to grasp and apply in code at first. So if you don’t understand it or find it complicated, that’s quite normal, and I’ll do my best to explain it and provide sample problems and code for you to practice with!

First, what exactly is a hash function?
Consider the following scenario: you work as a cashier, and someone comes to you asking for the price of an apple, and the prices of the things are kept in a book📕. Assuming they are alphabetically organized, you would have to look for the letter and search through all the words to find it. And, as some might know, that is O(n) time. What if I told you that we can utilize anything and make it constant time O(1), so that you can say “apple” and it will tell you exactly where its pricing is! That is where Hash functions come in.

A hash function is a function that accepts a string and returns a number. Each string, however, has its own number, so if apple is 5 and you call apple again, it should remain 5, otherwise there is a problem. In the best-case scenario, each string should correspond to a separate integer. So, with hashing, your apple has a number called “hash code,” and you call that number when you want an apple. Sometimes two objects with the same value have a collision, which I would recommend looking up and learning about (Google is a developer’s best friend, so get used to it!!).
As a result, hashing is quite useful for discovering and retrieving information!

“Hashing is a technique used in computer science to efficiently store and retrieve data. It involves converting data (such as strings or numbers) into fixed-size values called hash codes. These hash codes are used as indexes to store the data in data structures like hash maps, hash tables, and hash sets.”

What is Hash map and what is Hash table?

So, hash map and a hash table are both the the same they ways to store and organize information so we can find it quickly as we mentioned above. And we can implement them using dictionary in python {} .

Example:

# Creating a hash table to store fruits based on their first letter
hash_table = {}
hash_table['A'] = 'Apple'
hash_table['B'] = 'Banana'
hash_table['O'] = 'Orange'

# Accessing a fruit based on its first letter
print(hash_table['A']) # Output: 'Apple'
print(hash_table['B']) # Output: 'Banana'
print(hash_table['O']) # Output: 'Orange'

What is hash set?

As some might know sets in python can contain only unique elements. “Similar to hash table, a hash set is also a collection of objects. In hash table, data was stored in the form of key-value pairs, whereas in hash sets, the data is stored as objects. A hash set internally uses the hash table data structure to store data items. Just like a set, a hash set also does not allow storage of duplicate elements.”

Example regular set:

# Regular Set (Without Hashing)
my_set = {3, 7, 1, 9, 5}

# Searching for an element in the set
search_element = 7
if search_element in my_set:
print("Element found in the set!")
else:
print("Element not found in the set.")

------------------------------------------------------
Example hashset:

# Hash Set (With Hashing)
my_hash_set = set()

# Adding elements to the hash set
my_hash_set.add(3)
my_hash_set.add(7)
my_hash_set.add(1)
my_hash_set.add(9)
my_hash_set.add(5)

# Searching for an element in the hash set
search_element = 7
if search_element in my_hash_set:
print("Element found in the hash set!")
else:
print("Element not found in the hash set.")

When do we use which?

So, when solving Leetcode problems, we need to know when to use a hash map (hash table) and when to use a hash set.

For Hash Map(Hash Table):

  • Use a hash map (hash table) when you need to store key-value pairs and efficiently retrieve values based on their keys.
  • A common use case is when you need to track frequencies, counts, or occurrences of elements in an array or list.
  • Example of Leetcode problems:
  • Two sum & Most Common Word
#Two Sum:
For two sum for example the reason we can make use of a hashmap is because
using a hash map, you can store the elements of the array along with their
indices as key-value pairs. Then, while iterating through the array,
you can quickly check if the complement (target minus the current element)
exists in the hash map. If it does, you have found the pair that sums to the
target.

For Hash Sets:

  • Use a hash set when you need to efficiently check for the presence of elements in a collection and ensure uniqueness (no duplicates).
  • A common use case is when you want to find unique elements or identify whether there is any overlap between two collections.
  • Example of Leetcode problems:
  • Single Number & Intersection of Two Arrays

Time and Space complexity for hash tables:

Quiz:

Here are three problems and I want you to think of whether we need to use a hash map or a hash set for it. Ill have the answers for it in the bottom!

a) Storing a list of unique usernames for a social media platform.

b) Counting the occurrences of words in a book.

c) Keeping track of the prices of various products in an online store.

Also, are additional two videos for you:

Answers:

a) Hash Set

b) Hash Map

c) Hash Map

Thank you for reading this, I hope it helped and was simple to read. You also can read my other posts if you’re interested!

À bientôt👋

--

--