HashTable Data Structure

HashTable Data Structure

Hey Folks, in today's article i'll be sharing the basics of hash tables, What it is? alongside a practical example for a better understanding. I had a hard time understanding the whole concept of hash tables and Finally could make sense of it and wanted to share with anyone out there struggling with understand what they are.

So basically, A hash table is a data structure that stores data in an associative array format(Think of it like an array), which can be accessed using keys(unique). Each key is associated with a value, which can be anything from a simple data type, such as an integer, to a more complex data type, such as a string. Hash tables are used to store data in a way that makes it easy to retrieve, update, and delete. In some cases it's often used to store data in a database, as it can be used to store data in a way that makes it easy to index and search through.

How Does a Hash Table Work?

Data stored in a hash table basically has two main components, a key and a value. The key is used to access the item and the value is the data/information.

Example:

Lets say we want to create a hash table to store peoples' names and address etc. In this case a hash table is a way we can store that information into some sort of an associative array and we will map the key: name, to the value: address. Representing that visually we basically have an array structure as seen below;

HashTable Data Structure

Note: In a real life code implementation of hash table, we usually has a larger amount of data than what we are currently illustrating on the image above, just for the sake of understanding we picked a small dataset.

So, the way this works is, we are basically going to have a hash function. and what the hash function will do is look at certain key passed in and will essentially evaluate that key and will spit-out some sort of index number where that key's data will be stored.

For example: Typically; Hash(key)->index

with this, we could do; Hash(kelly)-> ? and from here, say our hash function has evaluated the key and it said, okay... that should go at index 2. which will be;

HashTable Data Structure

Hash(kelly)-> 2

One thing to be noted is, the way the hash function is written, if you enter the same key, it would spit-out the same index number. So every time i enter kelly into my hash function i should get the index 2. and the same process goes on and on;

HashTable Data Structure

Hash(fotie)-> 0 Hash(ben)-> 1 Hash(kelly)-> 2 Hash(jane)-> 3 Hash(george)-> 4

That's in a nutshell how it works, hope that helps;

Having anything to add to this? please do not hesitate to do so below:)