Hash-Table

Let’s learn about hash tables in Go.

Introduction

A hash table is a data structure that associates keys with values. The hash table calculates the index of the data in an array using a hash function. We employ the hash table when the number of key-value pairs saved is reasonably smaller than the number of available keys.

Process to store data

  • Firstly, create an array, referred to as a hash table.
  • Determine the hash code of a key using a hash function
  • Take the modulo of the hash code by the size of hash table to get the index where the hash table will store the data.
  • Finally, save this information in the appropriate index.

Process of searching data

  • We can generate a hash code for the key we’re looking for by using the hash function.
  • Take the modulo of the hash code by the hash table size to get the index of the table where the data is stored.
  • Finally, get the value from the index that we calculated.

Let’s look at the illustration of the hash table.

Get hands-on with 1400+ tech skills courses.