C Load Factor and Rehashing

Intermediate
6 min read
Updated: Nov 2026
1 example

Growing the Table

The load factor is count / capacity. As it rises, probes and chains get longer. A common rule is to rehash when it exceeds about 0.75: allocate a table twice as large and reinsert every key, because each key's slot depends on the capacity. Doubling keeps the amortized insert cost O(1).

Remember
load = (double)count / capacity;
if (load > 0.75) {
    /* new table, capacity * 2, reinsert every key */
}

Cheat Sheet

Keep these facts handy while you work with Load Factor and Rehashing in C.

TopicDetail
Load factorcount / capacity
Typical limit0.7 to 0.75
RehashReinsert all keys
AmortizedO(1) per insert

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Graphs.

Example 1 — Rehash while inserting 1 to 7

Start with four slots and linear probing.

C
#include <stdio.h>
#include <stdlib.h>
#define EMPTY (-1)

struct Set { int *slot; int cap, count; };

static int *make(int cap) {
    int *a = malloc(cap * sizeof *a);
    if (!a) exit(1);
    for (int i = 0; i < cap; i++) a[i] = EMPTY;
    return a;
}

static void place(int *slot, int cap, int key) {
    int i = key % cap;
    while (slot[i] != EMPTY) i = (i + 1) % cap;
    slot[i] = key;
}

static void add(struct Set *s, int key) {
    place(s->slot, s->cap, key);
    s->count++;
    if ((double)s->count / s->cap > 0.75) {
        int newcap = s->cap * 2;
        int *ns = make(newcap);
        for (int i = 0; i < s->cap; i++)
            if (s->slot[i] != EMPTY) place(ns, newcap, s->slot[i]);
        printf("rehash at key %d: capacity %d -> %d\n", key, s->cap, newcap);
        free(s->slot);
        s->slot = ns;
        s->cap = newcap;
    }
}

int main(void) {
    struct Set s = {make(4), 4, 0};
    for (int k = 1; k <= 7; k++) add(&s, k);

    printf("final: count=%d capacity=%d load=%.2f\n",
           s.count, s.cap, (double)s.count / s.cap);
    free(s.slot);
    return 0;
}

How It Works

  1. With four slots, the fourth key makes the load 1.0, which exceeds 0.75.
  2. All keys are reinserted into the 8-slot table, where key % 8 gives new slots.
  3. At the seventh key the load is 0.875, so the table doubles to 16, giving a final load of 0.4375.

Edge Cases & Pitfalls

Check these before relying on Load Factor and Rehashing in real programs.

copy

Copying slots instead of rehashing

Slots depend on capacity, so entries must be rehashed, not copied.

leak

Forgetting to free the old array

Free the previous slots after the new table is filled.

Key Takeaways

  • Watch count / capacity.
  • Double and reinsert.
  • Amortized O(1).

One line: The load factor is count / capacity.

Frequently Asked Questions

The ratio of stored entries to table capacity.
A key's slot is computed with the table size, so changing the size changes where each key belongs.
Continue with "C Graphs" next. Represent and traverse graphs in C with adjacency matrices and lists, BFS, and DFS. The C Hash Tables hub lists every topic in order.

Next: Graphs

Represent and traverse graphs in C with adjacency matrices and lists, BFS, and DFS.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful