PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Hashing in Data Structures

Hashing in Data Structures

Have you ever used hashing in your projects or played fun games build around it and wondered what can be the other uses of it and how it can make our apps and games much faster ?

What is hashing by the way?

hashing is a simple technique by which you can convert a convert a value into another by using a simple mathematical function.

\\ this is a example hash function
int hash(int x)
{
int k = (x+10)*1000;
return k;
}

now this hash function will mask the input and if you never know what the function was you will never get to know what was the input was.

Where we actually use hashing ?

  • In storing passwords

well the websites never store your passwords in plane text format. your password is always stored in a hashed format by a hashed function , so even if your account gets hacked , hackers won't be able to see it. (well i am going to write another article about it very soon)

  • While you login in a site

well websites never store your password in a simple plane format so each time you login the site these websites convert your password into a hash and check the hash they have stored previously. that's how you login

  • In string searching

Well for most of the string searching we use the Rabin-Karp algorithm which again use hashing.

Hash Table

Well , Hash Table is the Data Structure made of hashing. We store the data in a key value pair in hashing . Here we use a hash function to calculate the index of a array in which the element can be inserted.

** If we use a good hash function Under reasonable assumptions the average time required is O(1) **

Group 2 (2).png

Since we are using a hash function it might be possible that the data is arranged in a random manner.

How to actually use HashTable in our program ?

Well , We can use Unordered Map in our program if we want to use a hash table in our program since, Unordered map are implemented using hash table only and usually the Time complexity is O(1) for most of the operations.

well i would be using cpp to show you a small brief of it.

#include<iostream>
#include <bits/stdc++.h>
using namespace std;


int main(){
    
    unordered_map<string, int> Devdojo;
    
    //insert
    Devdojo["shadowBlade"] = 20;
    Devdojo.insert({"meow",30});
    //update
        Devdojo["shadowBlade"]++ ;
        Devdojo["shadowBlade"] = 25;
    //erase
     Devdojo.insert({"hi",30});
     Devdojo.erase("hi");
    //itreate
        for (auto x : Devdojo) 
      cout << x.first << " " << x.second << endl; 

     


    
    
    return 0;
}

wrap up

if you do like what i have written and want more similar post on such topic let me on twitter

and maybe you can support me on

Buy Me A Coffee tewarig

Comments (0)

loading comments