Skip to content

6.1   Hash table

A hash table achieves efficient element querying by establishing a mapping between keys and values. Specifically, when we input a key into the hash table, we can retrieve the corresponding value in \(O(1)\) time.

As shown in Figure 6-1, given \(n\) students, each with two pieces of data: "name" and "student number". If we want to implement a query feature that returns the corresponding name when given a student number, we can use the hash table shown in Figure 6-1.

Abstract representation of a hash table

Figure 6-1   Abstract representation of a hash table

Apart from hash tables, arrays and linked lists can also be used to implement querying functions. Their efficiency is compared in Table 6-1.

  • Adding elements: Simply add the element to the end of the array (or linked list), using \(O(1)\) time.
  • Querying elements: Since the array (or linked list) is unordered, it requires traversing all the elements, using \(O(n)\) time.
  • Deleting elements: First, locate the element, then delete it from the array (or linked list), using \(O(n)\) time.

Table 6-1   Comparison of element query efficiency

Array Linked List Hash Table
Find Element \(O(n)\) \(O(n)\) \(O(1)\)
Add Element \(O(1)\) \(O(1)\) \(O(1)\)
Delete Element \(O(n)\) \(O(n)\) \(O(1)\)

Observations reveal that the time complexity for adding, deleting, and querying in a hash table is \(O(1)\), which is highly efficient.

6.1.1   Common operations of hash table

Common operations of a hash table include initialization, querying, adding key-value pairs, and deleting key-value pairs, etc. Example code is as follows:

hash_map.py
# Initialize hash table
hmap: dict = {}

# Add operation
# Add key-value pair (key, value) to the hash table
hmap[12836] = "Xiao Ha"
hmap[15937] = "Xiao Luo"
hmap[16750] = "Xiao Suan"
hmap[13276] = "Xiao Fa"
hmap[10583] = "Xiao Ya"

# Query operation
# Input key into hash table, get value
name: str = hmap[15937]

# Delete operation
# Delete key-value pair (key, value) from hash table
hmap.pop(10583)
hash_map.cpp
/* Initialize hash table */
unordered_map<int, string> map;

/* Add operation */
// Add key-value pair (key, value) to the hash table
map[12836] = "Xiao Ha";
map[15937] = "Xiao Luo";
map[16750] = "Xiao Suan";
map[13276] = "Xiao Fa";
map[10583] = "Xiao Ya";

/* Query operation */
// Input key into hash table, get value
string name = map[15937];

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.erase(10583);
hash_map.java
/* Initialize hash table */
Map<Integer, String> map = new HashMap<>();

/* Add operation */
// Add key-value pair (key, value) to the hash table
map.put(12836, "Xiao Ha");   
map.put(15937, "Xiao Luo");   
map.put(16750, "Xiao Suan");   
map.put(13276, "Xiao Fa");
map.put(10583, "Xiao Ya");

/* Query operation */
// Input key into hash table, get value
String name = map.get(15937);

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.remove(10583);
hash_map.cs
/* Initialize hash table */
Dictionary<int, string> map = new() {
    /* Add operation */
    // Add key-value pair (key, value) to the hash table
    { 12836, "Xiao Ha" },
    { 15937, "Xiao Luo" },
    { 16750, "Xiao Suan" },
    { 13276, "Xiao Fa" },
    { 10583, "Xiao Ya" }
};

/* Query operation */
// Input key into hash table, get value
string name = map[15937];

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.Remove(10583);
hash_map_test.go
/* Initialize hash table */
hmap := make(map[int]string)

/* Add operation */
// Add key-value pair (key, value) to the hash table
hmap[12836] = "Xiao Ha"
hmap[15937] = "Xiao Luo"
hmap[16750] = "Xiao Suan"
hmap[13276] = "Xiao Fa"
hmap[10583] = "Xiao Ya"

/* Query operation */
// Input key into hash table, get value
name := hmap[15937]

/* Delete operation */
// Delete key-value pair (key, value) from hash table
delete(hmap, 10583)
hash_map.swift
/* Initialize hash table */
var map: [Int: String] = [:]

/* Add operation */
// Add key-value pair (key, value) to the hash table
map[12836] = "Xiao Ha"
map[15937] = "Xiao Luo"
map[16750] = "Xiao Suan"
map[13276] = "Xiao Fa"
map[10583] = "Xiao Ya"

/* Query operation */
// Input key into hash table, get value
let name = map[15937]!

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.removeValue(forKey: 10583)
hash_map.js
/* Initialize hash table */
const map = new Map();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.set(12836, 'Xiao Ha');
map.set(15937, 'Xiao Luo');
map.set(16750, 'Xiao Suan');
map.set(13276, 'Xiao Fa');
map.set(10583, 'Xiao Ya');

/* Query operation */
// Input key into hash table, get value
let name = map.get(15937);

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.delete(10583);
hash_map.ts
/* Initialize hash table */
const map = new Map<number, string>();
/* Add operation */
// Add key-value pair (key, value) to the hash table
map.set(12836, 'Xiao Ha');
map.set(15937, 'Xiao Luo');
map.set(16750, 'Xiao Suan');
map.set(13276, 'Xiao Fa');
map.set(10583, 'Xiao Ya');
console.info('\nAfter adding, the hash table is\nKey -> Value');
console.info(map);

/* Query operation */
// Input key into hash table, get value
let name = map.get(15937);
console.info('\nInput student number 15937, query name ' + name);

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.delete(10583);
console.info('\nAfter deleting 10583, the hash table is\nKey -> Value');
console.info(map);
hash_map.dart
/* Initialize hash table */
Map<int, String> map = {};

/* Add operation */
// Add key-value pair (key, value) to the hash table
map[12836] = "Xiao Ha";
map[15937] = "Xiao Luo";
map[16750] = "Xiao Suan";
map[13276] = "Xiao Fa";
map[10583] = "Xiao Ya";

/* Query operation */
// Input key into hash table, get value
String name = map[15937];

/* Delete operation */
// Delete key-value pair (key, value) from hash table
map.remove(10583);
hash_map.rs
use std::collections::HashMap;

/* Initialize hash table */
let mut map: HashMap<i32, String> = HashMap::new();

/* Add operation */
// Add key-value pair (key, value) to the hash table
map.insert(12836, "Xiao Ha".to_string());
map.insert(15937, "Xiao Luo".to_string());
map.insert(16750, "Xiao Suan".to_string());
map.insert(13279, "Xiao Fa".to_string());
map.insert(10583, "Xiao Ya".to_string());

/* Query operation */
// Input key into hash table, get value
let _name: Option<&String> = map.get(&15937);

/* Delete operation */
// Delete key-value pair (key, value) from hash table
let _removed_value: Option<String> = map.remove(&10583);
hash_map.c
// C does not provide a built-in hash table
hash_map.kt

hash_map.zig

Code Visualization

There are three common ways to traverse a hash table: traversing key-value pairs, keys, and values. Example code is as follows:

hash_map.py
# Traverse hash table
# Traverse key-value pairs key->value
for key, value in hmap.items():
    print(key, "->", value)
# Traverse keys only
for key in hmap.keys():
    print(key)
# Traverse values only
for value in hmap.values():
    print(value)
hash_map.cpp
/* Traverse hash table */
// Traverse key-value pairs key->value
for (auto kv: map) {
    cout << kv.first << " -> " << kv.second << endl;
}
// Traverse using iterator key->value
for (auto iter = map.begin(); iter != map.end(); iter++) {
    cout << iter->first << "->" << iter->second << endl;
}
hash_map.java
/* Traverse hash table */
// Traverse key-value pairs key->value
for (Map.Entry<Integer, String> kv: map.entrySet()) {
    System.out.println(kv.getKey() + " -> " + kv.getValue());
}
// Traverse keys only
for (int key: map.keySet()) {
    System.out.println(key);
}
// Traverse values only
for (String val: map.values()) {
    System.out.println(val);
}
hash_map.cs
/* Traverse hash table */
// Traverse key-value pairs Key->Value
foreach (var kv in map) {
    Console.WriteLine(kv.Key + " -> " + kv.Value);
}
// Traverse keys only
foreach (int key in map.Keys) {
    Console.WriteLine(key);
}
// Traverse values only
foreach (string val in map.Values) {
    Console.WriteLine(val);
}
hash_map_test.go
/* Traverse hash table */
// Traverse key-value pairs key->value
for key, value := range hmap {
    fmt.Println(key, "->", value)
}
// Traverse keys only
for key := range hmap {
    fmt.Println(key)
}
// Traverse values only
for _, value := range hmap {
    fmt.Println(value)
}
hash_map.swift
/* Traverse hash table */
// Traverse key-value pairs Key->Value
for (key, value) in map {
    print("\(key) -> \(value)")
}
// Traverse keys only
for key in map.keys {
    print(key)
}
// Traverse values only
for value in map.values {
    print(value)
}
hash_map.js
/* Traverse hash table */
console.info('\nTraverse key-value pairs Key->Value');
for (const [k, v] of map.entries()) {
    console.info(k + ' -> ' + v);
}
console.info('\nTraverse keys only Key');
for (const k of map.keys()) {
    console.info(k);
}
console.info('\nTraverse values only Value');
for (const v of map.values()) {
    console.info(v);
}
hash_map.ts
/* Traverse hash table */
console.info('\nTraverse key-value pairs Key->Value');
for (const [k, v] of map.entries()) {
    console.info(k + ' -> ' + v);
}
console.info('\nTraverse keys only Key');
for (const k of map.keys()) {
    console.info(k);
}
console.info('\nTraverse values only Value');
for (const v of map.values()) {
    console.info(v);
}
hash_map.dart
/* Traverse hash table */
// Traverse key-value pairs Key->Value
map.forEach((key, value) {
print('$key -> $value');
});

// Traverse keys only Key
map.keys.forEach((key) {
print(key);
});

// Traverse values only Value
map.values.forEach((value) {
print(value);
});
hash_map.rs
/* Traverse hash table */
// Traverse key-value pairs Key->Value
for (key, value) in &map {
    println!("{key} -> {value}");
}

// Traverse keys only Key
for key in map.keys() {
    println!("{key}"); 
}

// Traverse values only Value
for value in map.values() {
    println!("{value}");
}
hash_map.c
// C does not provide a built-in hash table
hash_map.kt

hash_map.zig
// Zig example is not provided
Code Visualization

6.1.2   Simple implementation of hash table

First, let's consider the simplest case: implementing a hash table using just an array. In the hash table, each empty slot in the array is called a bucket, and each bucket can store one key-value pair. Therefore, the query operation involves finding the bucket corresponding to the key and retrieving the value from it.

So, how do we locate the appropriate bucket based on the key? This is achieved through a hash function. The role of the hash function is to map a larger input space to a smaller output space. In a hash table, the input space is all possible keys, and the output space is all buckets (array indices). In other words, input a key, and we can use the hash function to determine the storage location of the corresponding key-value pair in the array.

The calculation process of the hash function for a given key is divided into the following two steps:

  1. Calculate the hash value using a certain hash algorithm hash().
  2. Take the modulus of the hash value with the number of buckets (array length) capacity to obtain the array index index.
index = hash(key) % capacity

Afterward, we can use index to access the corresponding bucket in the hash table and thereby retrieve the value.

Assuming array length capacity = 100 and hash algorithm hash(key) = key, the hash function is key % 100. Figure 6-2 uses key as the student number and value as the name to demonstrate the working principle of the hash function.

Working principle of hash function

Figure 6-2   Working principle of hash function

The following code implements a simple hash table. Here, we encapsulate key and value into a class Pair to represent the key-value pair.

array_hash_map.py
class Pair:
    """Key-value pair"""

    def __init__(self, key: int, val: str):
        self.key = key
        self.val = val

class ArrayHashMap:
    """Hash table based on array implementation"""

    def __init__(self):
        """Constructor"""
        # Initialize an array, containing 100 buckets
        self.buckets: list[Pair | None] = [None] * 100

    def hash_func(self, key: int) -> int:
        """Hash function"""
        index = key % 100
        return index

    def get(self, key: int) -> str:
        """Query operation"""
        index: int = self.hash_func(key)
        pair: Pair = self.buckets[index]
        if pair is None:
            return None
        return pair.val

    def put(self, key: int, val: str):
        """Add operation"""
        pair = Pair(key, val)
        index: int = self.hash_func(key)
        self.buckets[index] = pair

    def remove(self, key: int):
        """Remove operation"""
        index: int = self.hash_func(key)
        # Set to None, representing removal
        self.buckets[index] = None

    def entry_set(self) -> list[Pair]:
        """Get all key-value pairs"""
        result: list[Pair] = []
        for pair in self.buckets:
            if pair is not None:
                result.append(pair)
        return result

    def key_set(self) -> list[int]:
        """Get all keys"""
        result = []
        for pair in self.buckets:
            if pair is not None:
                result.append(pair.key)
        return result

    def value_set(self) -> list[str]:
        """Get all values"""
        result = []
        for pair in self.buckets:
            if pair is not None:
                result.append(pair.val)
        return result

    def print(self):
        """Print hash table"""
        for pair in self.buckets:
            if pair is not None:
                print(pair.key, "->", pair.val)
array_hash_map.cpp
/* Key-value pair */
struct Pair {
  public:
    int key;
    string val;
    Pair(int key, string val) {
        this->key = key;
        this->val = val;
    }
};

/* Hash table based on array implementation */
class ArrayHashMap {
  private:
    vector<Pair *> buckets;

  public:
    ArrayHashMap() {
        // Initialize an array, containing 100 buckets
        buckets = vector<Pair *>(100);
    }

    ~ArrayHashMap() {
        // Free memory
        for (const auto &bucket : buckets) {
            delete bucket;
        }
        buckets.clear();
    }

    /* Hash function */
    int hashFunc(int key) {
        int index = key % 100;
        return index;
    }

    /* Query operation */
    string get(int key) {
        int index = hashFunc(key);
        Pair *pair = buckets[index];
        if (pair == nullptr)
            return "";
        return pair->val;
    }

    /* Add operation */
    void put(int key, string val) {
        Pair *pair = new Pair(key, val);
        int index = hashFunc(key);
        buckets[index] = pair;
    }

    /* Remove operation */
    void remove(int key) {
        int index = hashFunc(key);
        // Free memory and set to nullptr
        delete buckets[index];
        buckets[index] = nullptr;
    }

    /* Get all key-value pairs */
    vector<Pair *> pairSet() {
        vector<Pair *> pairSet;
        for (Pair *pair : buckets) {
            if (pair != nullptr) {
                pairSet.push_back(pair);
            }
        }
        return pairSet;
    }

    /* Get all keys */
    vector<int> keySet() {
        vector<int> keySet;
        for (Pair *pair : buckets) {
            if (pair != nullptr) {
                keySet.push_back(pair->key);
            }
        }
        return keySet;
    }

    /* Get all values */
    vector<string> valueSet() {
        vector<string> valueSet;
        for (Pair *pair : buckets) {
            if (pair != nullptr) {
                valueSet.push_back(pair->val);
            }
        }
        return valueSet;
    }

    /* Print hash table */
    void print() {
        for (Pair *kv : pairSet()) {
            cout << kv->key << " -> " << kv->val << endl;
        }
    }
};
array_hash_map.java
/* Key-value pair */
class Pair {
    public int key;
    public String val;

    public Pair(int key, String val) {
        this.key = key;
        this.val = val;
    }
}

/* Hash table based on array implementation */
class ArrayHashMap {
    private List<Pair> buckets;

    public ArrayHashMap() {
        // Initialize an array, containing 100 buckets
        buckets = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            buckets.add(null);
        }
    }

    /* Hash function */
    private int hashFunc(int key) {
        int index = key % 100;
        return index;
    }

    /* Query operation */
    public String get(int key) {
        int index = hashFunc(key);
        Pair pair = buckets.get(index);
        if (pair == null)
            return null;
        return pair.val;
    }

    /* Add operation */
    public void put(int key, String val) {
        Pair pair = new Pair(key, val);
        int index = hashFunc(key);
        buckets.set(index, pair);
    }

    /* Remove operation */
    public void remove(int key) {
        int index = hashFunc(key);
        // Set to null, indicating removal
        buckets.set(index, null);
    }

    /* Get all key-value pairs */
    public List<Pair> pairSet() {
        List<Pair> pairSet = new ArrayList<>();
        for (Pair pair : buckets) {
            if (pair != null)
                pairSet.add(pair);
        }
        return pairSet;
    }

    /* Get all keys */
    public List<Integer> keySet() {
        List<Integer> keySet = new ArrayList<>();
        for (Pair pair : buckets) {
            if (pair != null)
                keySet.add(pair.key);
        }
        return keySet;
    }

    /* Get all values */
    public List<String> valueSet() {
        List<String> valueSet = new ArrayList<>();
        for (Pair pair : buckets) {
            if (pair != null)
                valueSet.add(pair.val);
        }
        return valueSet;
    }

    /* Print hash table */
    public void print() {
        for (Pair kv : pairSet()) {
            System.out.println(kv.key + " -> " + kv.val);
        }
    }
}
array_hash_map.cs
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.go
[class]{pair}-[func]{}

[class]{arrayHashMap}-[func]{}
array_hash_map.swift
[file]{utils/pair.swift}-[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.js
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.ts
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.dart
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.rs
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.c
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.kt
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.rb
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}
array_hash_map.zig
[class]{Pair}-[func]{}

[class]{ArrayHashMap}-[func]{}

6.1.3   Hash collision and resizing

Fundamentally, the role of the hash function is to map the entire input space of all keys to the output space of all array indices. However, the input space is often much larger than the output space. Therefore, theoretically, there must be situations where "multiple inputs correspond to the same output".

For the hash function in the above example, if the last two digits of the input key are the same, the output of the hash function will also be the same. For example, when querying for students with student numbers 12836 and 20336, we find:

12836 % 100 = 36
20336 % 100 = 36

As shown in Figure 6-3, both student numbers point to the same name, which is obviously incorrect. This situation where multiple inputs correspond to the same output is known as hash collision.

Example of hash collision

Figure 6-3   Example of hash collision

It is easy to understand that the larger the capacity \(n\) of the hash table, the lower the probability of multiple keys being allocated to the same bucket, and the fewer the collisions. Therefore, expanding the capacity of the hash table can reduce hash collisions.

As shown in Figure 6-4, before expansion, key-value pairs (136, A) and (236, D) collided; after expansion, the collision is resolved.

Hash table expansion

Figure 6-4   Hash table expansion

Similar to array expansion, resizing a hash table requires migrating all key-value pairs from the original hash table to the new one, which is time-consuming. Furthermore, since the capacity capacity of the hash table changes, we need to recalculate the storage positions of all key-value pairs using the hash function, which adds to the computational overhead of the resizing process. Therefore, programming languages often reserve a sufficiently large capacity for the hash table to prevent frequent resizing.

The load factor is an important concept for hash tables. It is defined as the ratio of the number of elements in the hash table to the number of buckets. It is used to measure the severity of hash collisions and is often used as a trigger for resizing the hash table. For example, in Java, when the load factor exceeds \(0.75\), the system will resize the hash table to twice its original size.

Feel free to drop your insights, questions or suggestions