reading-notes


Project maintained by Razan-am Hosted on GitHub Pages — Theme by mattgraham

Hash Tables

What is Hashing?

Why do we use them?

  1. Hold unique values
  2. Dictionary
  3. Library

In universities, each student is assigned a unique roll number that can be used to retrieve information about them.

In libraries, each book is assigned a unique number that can be used to determine information about the book, such as its exact position in the library or the users it has been issued to etc.

What Are they

Hash function

Steps to implemented Hashing

  1. An element is converted into an integer by using a hash function. This element can be used as an index to store the original element, which falls into the hash table.
  2. The element is stored in the hash table where it can be quickly retrieved using hashed key.

Buckets

Collisions

Creating a Hash

  1. Add or multiply all the ASCII values together.
  2. Multiply it by a prime number such as 599.
  3. Use modulo to get the remainder of the result, when divided by the total size of the array.
  4. Insert into the array at that index.

Example:

Key = “Cat” Value = “Josie”

67 + 97 + 116 = 280

280 * 599 = 69648

69648 % 1024 = 16

Key gets placed in index of 16.

We now know that our key Cat maps to index 16 of the array. We can view into this index and find Josie, our value quickly.

Hash maps do this to store values

Hash maps do this to read value:

Internal Methods


References:

@By codefellows/Hashtables

@By hackerearth/Basics of Hash Tables