hashTable.definition

hashTable.descriptionPart1

hashTable.descriptionPart2

hashTable.keyPointsTitle

  • hashTable.keyPoint1
  • hashTable.keyPoint2
  • hashTable.keyPoint3
  • hashTable.keyPoint4

hashTable.exampleTitle

0
112
2
325
437
5
648

hashTable.exampleCaption

hashTable.collisionMethodsTitle

hashTable.collisionMethods.linear

h(k,i) = (h(k) + i) mod m

hashTable.linearDesc

hashTable.collisionMethods.quadratic

h(k,i) = (h(k) + i²) mod m

hashTable.quadraticDesc

hashTable.collisionMethods.double

h(k,i) = (h₁(k) + i×h₂(k)) mod m

hashTable.doubleDesc

hashTable.syntaxTitle

// Java
class HashTable {
    Integer[] table;
    boolean[] deleted;
    int size;

    HashTable(int capacity) {
        table = new Integer[capacity];
        deleted = new boolean[capacity];
        size = 0;
    }

    int hash(int key) {
        return ((key % table.length) + table.length) % table.length;
    }

    void insert(int key) {
        int index = hash(key);
        int i = 0;
        while (table[index] != null && !deleted[index]) {
            i++;
            index = (hash(key) + i) % table.length; // Linear probing
        }
        table[index] = key;
        deleted[index] = false;
        size++;
    }
}
# Python
class HashTable:
    def __init__(self, capacity):
        self.table = [None] * capacity
        self.deleted = [False] * capacity
        self.size = 0

    def hash(self, key):
        return key % len(self.table)

    def insert(self, key):
        index = self.hash(key)
        i = 0
        while self.table[index] is not None and not self.deleted[index]:
            i += 1
            index = (self.hash(key) + i) % len(self.table)  # Linear probing
        self.table[index] = key
        self.deleted[index] = False
        self.size += 1
// C++
class HashTable {
    vector<int> table;
    vector<bool> occupied;
    vector<bool> deleted;
    int capacity;

public:
    HashTable(int cap) : capacity(cap) {
        table.resize(cap);
        occupied.resize(cap, false);
        deleted.resize(cap, false);
    }

    int hash(int key) {
        return ((key % capacity) + capacity) % capacity;
    }

    void insert(int key) {
        int index = hash(key);
        int i = 0;
        while (occupied[index] && !deleted[index]) {
            i++;
            index = (hash(key) + i) % capacity; // Linear probing
        }
        table[index] = key;
        occupied[index] = true;
        deleted[index] = false;
    }
};
// JavaScript
class HashTable {
  constructor(capacity) {
    this.table = new Array(capacity).fill(null);
    this.deleted = new Array(capacity).fill(false);
    this.size = 0;
  }

  hash(key) {
    return ((key % this.table.length) + this.table.length) % this.table.length;
  }

  insert(key) {
    let index = this.hash(key);
    let i = 0;
    while (this.table[index] !== null && !this.deleted[index]) {
      i++;
      index = (this.hash(key) + i) % this.table.length; // Linear probing
    }
    this.table[index] = key;
    this.deleted[index] = false;
    this.size++;
  }
}

Interactive Hash Table Visualization

Hash Table (Size: 11)

0
1
2
3
4
5
6
7
8
9
10
Size:11
Occupied:0
Load Factor:0.00
Method:Linear
Empty
Occupied
Deleted
Hashing
Probing
Collision
Found

Note: Hash function: h(k) = k mod 11. Linear Probing: h(k,i) = (h(k) + i) mod m

enhanced.realWorldExamples.title

Database SystemsIndex Lookup

Databases use hash tables for O(1) primary key lookups.

Why? Constant-time access to any record by key.

CompilersSymbol Tables

Compilers use hash tables to store variable/function names.

Why? Fast lookup of identifiers during compilation.

Time Complexity

OperationAverage CaseWorst CaseDescription
InsertO(1)O(n)Compute hash, probe for empty slot if collision
SearchO(1)O(n)Compute hash, probe until found or empty slot
DeleteO(1)O(n)Search then mark as deleted (tombstone)
Resize/RehashO(n)O(n)Create new table and reinsert all elements

Load factor (α): Ratio of elements to table size. Keep α < 0.7 for good performance.

Prime table size: Using prime numbers reduces clustering and improves distribution.

Space complexity: O(n) for storing n elements. Open addressing uses less memory than chaining.

enhanced.complexityAnalysis.title

enhanced.complexityAnalysis.mathematicalTitle

With a good hash function and low load factor, collisions are rare. Expected number of comparisons approaches 1, giving O(1) average case.

🟢Best Case

O(1) when hash directly maps to empty slot with no collision.

🟡Average Case

O(1) for insert, delete, and search with good hash function and load factor < 0.75.

🔴Worst Case

O(n) when all keys hash to same bucket (extremely poor hash function or adversarial input).

💾Space Complexity

O(n) with typical load factor of 0.75 meaning ~33% extra space overhead.

enhanced.relatedTopics.prerequisites

enhanced.relatedTopics.related

enhanced.relatedTopics.advanced

hashTable.continueLearning

enhanced.author.title

enhanced.author.writtenBy:enhanced.author.teamName
enhanced.author.reviewedBy:enhanced.author.reviewerName
enhanced.author.lastUpdated:January 29, 2026