Hash tables are data structures that serve as efficient key-value stores. They are capable of assigning and retrieving data in the fastest way possible. This is because the underlying data structure that hash tables use is an array.

A value is stored at an array index determined by plugging the key into a hash function. Because we always know exactly where to find values in a hash table, we have constant-time access to any of the values it contains.

This quick access to values makes hash tables a good choice of data structure whenever we need to store a lot of values but need fast look-up time.

In this lesson, you will create your own implementation of a hash table by building out a HashTable structure. You’ll build methods to hash and access key-value pairs, assign an index at which to store a value and retrieve stored values.

To implement a hash table, the HashTable initializer will create an empty array to hold values. You’ll determine where to store values in the hash table by using a hash value. While going through the following exercises, remember that the purpose of this lesson is to gain a deeper understanding of the data structure rather than creating a production-quality data structure.



The HashTable structure we will be completing is stored in the HashTable.swift file. Look over the code you’ve been given which includes the initializer method.

Hash Tables are based on arrays, so whenever we create a new HashTable instance, the initializer creates a String array filled with empty string values that can be replaced with key-value pairs.

Click Next when you’re ready to move on!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?