Skip to main content

Hash Tables Explained


Hash Table

A Hash Table is a data structure designed to be fast to work with.

Hash Tables are often preferred over arrays or linked lists because searching, adding, and deleting data can be done very quickly, even for large amounts of data.

Why Not Arrays or Linked Lists?

In a Linked List, finding a person like "Bob" means checking each node one by one until Bob is found.

In an Array, finding an element is fast only if we know its index. If we only know the value (like a name), we must compare each element.

A Hash Table avoids this by letting us go directly to the correct location using a hash function.

Building a Hash Table from Scratch

We will build a simple Hash Set to store unique names.

Step 1: Start with an Array

my_array = ['Pete', 'Jones', 'Lisa', 'Bob', 'Siri']

Searching this array for "Bob" requires checking elements one by one.

Instead, we create a fixed-size array of buckets:

my_hash_set = [None, None, None, None, None,
               None, None, None, None, None]

Step 2: Storing Names Using a Hash Function

A hash function converts a value into an index number.

def hash_function(value):
    sum_of_chars = 0
    for char in value:
        sum_of_chars += ord(char)
    return sum_of_chars % 10

Example for "Bob":

  • B → 66
  • o → 111
  • b → 98

Total = 275 → 275 % 10 = 5

So "Bob" is stored at index 5.

my_hash_set = [None, None, None, None, None,
               'Bob', None, None, None, None]

Step 3: Looking Up a Name

To check if "Pete" exists:

  1. Run the hash function on "Pete"
  2. Get index 8
  3. Check bucket 8 directly
def contains(name):
    index = hash_function(name)
    return my_hash_set[index] == name

Step 4: Handling Collisions

A collision happens when two values get the same hash code.

Example:

  • "Lisa" → index 3
  • "Stuart" → index 3

Solution: Chaining (store multiple values in the same bucket).

my_hash_set = [
    [None],
    ['Jones'],
    [None],
    ['Lisa', 'Stuart'],
    [None],
    ['Bob'],
    [None],
    ['Siri'],
    ['Pete'],
    [None]
]

Step 5: Complete Hash Set Example

def add(value):
    index = hash_function(value)
    bucket = my_hash_set[index]
    if value not in bucket:
        bucket.append(value)

def contains(value):
    index = hash_function(value)
    return value in my_hash_set[index]

Uses of Hash Tables

  • Checking if an item exists in a collection
  • Storing unique values
  • Mapping keys to values (e.g., name → phone number)

Hash Tables are fast:

  • Arrays / Linked Lists → O(n)
  • Hash Tables (average) → O(1)

Hash Set vs Hash Map

Hash Set Hash Map
Stores only unique keys Stores key-value pairs
Checks if something exists Finds data using a key

Summary

  • Data is stored in buckets
  • A hash function decides the bucket
  • Collisions are normal and manageable
  • Hash Tables are extremely fast

Conclusion: Hash Tables allow fast storage, lookup, and deletion by using a hash function to jump directly to data.

Further Reading

People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *

Popular Posts

Rayleigh vs Rician Fading (with MATLAB + Simulator)

  In Rayleigh fading , the channel coefficients tend to have a Rayleigh distribution, which is characterized by a random phase and magnitude with an exponential distribution. This means the magnitude of the channel coefficient follows an exponential distribution with a mean of 1. In Rician fading , there is a dominant line-of-sight component in addition to the scattered components. The channel coefficients in Rician fading can indeed tend towards 1, especially when the line-of-sight component is strong. When the line-of-sight component dominates, the Rician fading channel behaves more deterministically, and the channel coefficients may tend towards the value of the line-of-sight component, which could be close to 1.   MATLAB Script clc; clear all; close all; % Define parameters numSamples = 1000; % Number of samples K_factor = 5; % K-factor for Rician fading SNR_dB = 20; % Signal-to-noise ratio (in dB) % Generate complex Gaussian random variable for Rayleigh fading channel h_r...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. 📘 Theory 🧮 Simulators 💻 MATLAB Code 📚 Resources BER Definition SNR Formula BER Calculator MATLAB Comparison 📂 Explore M-ary QAM, PSK, and QPSK Topics ▼ 🧮 Constellation Simulator: M-ary QAM 🧮 Constellation Simulator: M-ary PSK 🧮 BER calculation for ASK, FSK, and PSK 🧮 Approaches to BER vs SNR What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figure of merit for a...

UGC-NET Electronic Science Question Paper With Answer Key and Full Explanation [Dec 2023]

    UGC-NET Electronic Science Question Paper With Answer Key Download Pdf [Dec 2023] Download Question Paper               See Answers   2025 | 2024 | 2023 | 2022 | 2021 | 2020 UGC-NET Electronic Science  2023 Answers with Explanations 51. (A): The stacking fault is the most common area defect found in silicon. These faults typically occur along the 111 plane. In the crystalline structure of silicon, atoms are arranged in a specific pattern known as a diamond lattice. A stacking fault refers to a disruption in the normal order of atomic layers within this lattice, which usually occurs in the 111 plane due to the geometric arrangement of the atoms. This type of defect can affect the electrical and mechanical properties of the material, such as the mobility of charge carriers and mechanical strength. 52. (C): The important figure of merit for the microwave application of a Schot...

Theoretical vs. simulated BER vs. SNR for ASK, FSK, and PSK (MATLAB Code + Simulator)

📘 Overview 🧮 Simulator 💻 Theoretical Code 📊 Simulated Code 📚 Resources Overview BER vs. SNR denotes how many bits in error are received for a given signal-to-noise ratio, typically measured in dB. Common noise types in wireless systems: 🚀 1. Additive White Gaussian Noise (AWGN) 🌊 2. Rayleigh Fading AWGN adds random noise; Rayleigh fading attenuates the signal variably. A good SNR helps reduce these effects. Bit Error Rate (BER) Equations BER formulas for ASK, FSK, and PSK modulation schemes. ASK BER = 0.5 × erfc(0.5 × √SNR) FSK BER = 0.5 × erfc(√(SNR / 2)) PSK BER = 0.5 × erfc(√SNR) erfc / Q-function (Click here) Live BER S...

How to use MATLAB Simulink

Introduction to MATLAB Simulink MATLAB Simulink is a popular add-on of MATLAB. Here, you can use different blocks like modulator, demodulator, AWGN channel, etc. And you can do experiments on your own. Steps to Get Started 1. Go to the 'Simulink' tab at the top navbar of MATLAB. If not found, click on the add-on tab, search 'Simulink,' and then click on it to add. 2. Once you installed the simulation, click the 'new' tap at the top left corner. 3. Then, search the required blocks in the 'Simulink library.' Then, drag it to the editor space. 4. You can double-click on the blocks to see the input parameters. 5. Then, connect the blocks by dragging a line from one block's output terminal to another block's input. 6. If the connection is complete, click the 'run' tab in the middle of the top navbar. 7. After clicking on the run ...

UGC NET Electronic Science Previous Year Question Papers

Home / Engineering & Other Exams / UGC NET 2022 PYQ 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading UGC-NET (Electronics Science, Subject code: 88) Subject_Code : 88; Department : Electronic Science; 📂 View All Question Papers Q. UGC Net Electronic Science Question Paper [June 2025] A. UGC Net Electronic Science Question Paper With Answer Key Download Pdf [June 2025] with full explanation Q. UGC Net Electronic Science Question Paper [December 2024] A. UGC Net Electronic Science Question Paper With Answer Key Download Pdf [December 2024] Q. UGC Net Electronic Science Question Paper [Aug 2024] A. UGC Net Electronic Scien...

OFDM Waveform with MATLAB Code (with Simulator)

  In OFDM (Orthogonal Frequency Division Multiplexing) , we transmit multiple orthogonal subcarriers simultaneously. Since the subcarriers are orthogonal , they do not interfere with each other, which is one of the main advantages of OFDM. Practically, OFDM converts a wideband signal into multiple narrowband orthogonal subcarriers. For typical wireless communication, if the signal bandwidth (or symbol duration) exceeds the coherence bandwidth of the channel, the signal experiences frequency-selective fading . Fading distorts the signal, making it difficult to recover the original information. By using OFDM, we transmit the same wideband signal across multiple orthogonal narrowband subcarriers, reducing the effect of fading. For example, if we want to transmit a signal of bandwidth 1024 kHz , we can divide it into N = 8 subcarriers . Each subcarrier is then spaced by: Δf = Total Bandwidth N = 1024 8 kHz...

Constellation Diagrams of ASK, PSK, and FSK (with MATLAB Code + Simulator)

Constellation Diagrams: ASK, FSK, and PSK Comprehensive guide to signal space representation, including interactive simulators and MATLAB implementations. 📘 Overview 🧮 Simulator ⚖️ Theory 📚 Resources Definitions Constellation Tool Key Points MATLAB Code 📂 Other Topics: M-ary PSK & QAM Diagrams ▼ 🧮 Simulator for M-ary PSK Constellation 🧮 Simulator for M-ary QAM Constellation BASK (Binary ASK) Modulation Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1. BFSK (Binary FSK) Modulation Transmits on...