What is the 'Load Factor' in a Map?
Updated Apr 28, 2026
Short answer
The load factor of a map (hash table) measures how full the table is by comparing the number of stored entries to the number of available buckets. It is calculated as `number of entries / number of buckets`. A higher load factor saves memory but increases the chance of collisions, while a lower load factor improves performance but uses more memory.
Deep explanation
A map (also called a hash map or hash table) stores data as key-value pairs.
Example:
Key Value
"name" -> "Alice""age" -> 25"city" -> "London"Internally, a hash map uses an array of buckets. A hash function converts each key into an index where the value should be stored.
Example:
Hash Function:
"apple" -> 3"banana" -> 1"orange" -> 5
Buckets:
Index:0 1 2 3 4 5--------------------------------| |banana | |apple | |orange|--------------------------------The load factor tells us how much of this bucket array is occupied.
Load Factor Formula
The formula is:
Load Factor = Number of Stored Entries / Number of BucketsExample:
Entries stored = 7Number of buckets = 10
Load Factor = 7 / 10
Load Factor = 0.7This means the hash table is 70% full.
Why Load Factor Matters
As more elements are added, the number of entries increases while the number of buckets remains the same.
Example:
Initially:
Buckets:
0 1 2 3 4-----------------| | | | |-----------------
Entries: 0Load Factor: 0After adding elements:
Buckets:
0 1 2 3 4-----------------|A |B |C |D |-----------------
Entries: 4Load Factor: 4/5 = 0.8As the load factor increases, collisions become more likely.
Effect of High Load Factor
A high load factor means many entries are stored in a relatively small number of buckets.
Example:
Bucket 2:
[Key A] |[Key B] |[Key C]Multiple keys map to the same bucket. This is called a collision.
Hash maps handle collisions using techniques such as:
- Separate chaining: Store multiple entries in a linked list or tree inside a bucket.
- Open addressing: Find another empty location in the table.
Too many collisions reduce performance because searching within a bucket takes additional time.
Average complexity may change:
| Operation | Good Load Factor | Very High Load Factor |
|---|---|---|
| Insert | O(1) average | Can approach O(n) |
| Search | O(1) average | Can approach O(n) |
| Delete | O(1) average | Can approach O(n) |
Effect of Low Load Factor
A low load factor means fewer entries are stored relative to the number of buckets.
Example:
Entries = 2Buckets = 100
Load Factor = 2/100 = 0.02Advantages:
- Fewer collisions.
- Faster lookups.
Disadvantages:
- Wastes memory because many buckets remain empty.
Resizing and Rehashing
Most hash maps automatically resize when the load factor exceeds a certain threshold.
Example:
Initial:
Buckets = 10Entries = 8
Load Factor = 8/10 = 0.8If the threshold is 0.75, the map grows:
Old table:10 buckets
New table:20 bucketsThe entries are then redistributed using the hash function. This process is called rehashing.
Rehashing is expensive because every existing entry must be moved.
However, because resizing happens occasionally, insertion remains amortized `O(1)`.
Choosing a Load Factor
A typical load factor is around:
0.75This provides a balance between:
- Memory usage.
- Number of collisions.
- Lookup performance.
A lower load factor may be chosen when:
- Fast lookups are critical.
- Memory is available.
A higher load factor may be chosen when:
- Memory usage is more important.
- Slightly slower operations are acceptable.
Load Factor in Common Languages
Different programming languages use different default load factors.
Examples:
- Java's
HashMapcommonly uses a default load factor of0.75. - Other implementations may choose different values depending on their resizing strategy and performance goals.
The exact value is an implementation detail, but the trade-off remains the same: fewer collisions versus lower memory usage.
Real-world example
Consider a username lookup system.
A map stores usernames and user IDs:
users = { "alice": 101, "bob": 102, "charlie": 103}Internally, the map may have:
Buckets: 8Entries: 3
Load Factor = 3 / 8
Load Factor = 0.375The table has plenty of empty space, so collisions are unlikely and lookups are fast.
If thousands of users are added:
Buckets: 8Entries: 20
Load Factor = 20 / 8
Load Factor = 2.5The map becomes overcrowded, causing many collisions. The implementation will usually resize the table and redistribute the entries.
Common mistakes
- * Thinking load factor represents the percentage of memory used instead of the ratio of entries to buckets.
- * Assuming a higher load factor always improves performance because it uses fewer buckets.
- * Forgetting that a high load factor increases the chance of hash collisions.
- * Assuming resizing happens every time an element is inserted.
- * Confusing load factor with the size of the map.
- * Believing hash maps always have `O(1)` performance regardless of collisions.
- * Ignoring the memory trade-off when choosing a load factor.
- * Forgetting that rehashing temporarily takes `O(n)` time.
Follow-up questions
- Why does a high load factor increase collisions?
- What happens when a hash map exceeds its load factor threshold?
- Why is a load factor of around 0.75 commonly used?
- Does a lower load factor always make a hash map faster?
- What is the difference between load factor and capacity in a hash map?
- How does rehashing affect the complexity of inserting into a hash map?