LRU & LFU
Cache
A cache with O(1) get and O(1) put — including eviction. The secret: pair a hash map with a doubly-linked list so every operation is a constant-time pointer swap.
How do you implement an LRU cache?
An LRU (Least Recently Used) cache evicts the item that hasn't been used for the longest time when it's full. The classic O(1) design combines a hash map (key → node) with a doubly linked list ordered by recent use. In JavaScript, a Map already keeps insertion order, so you can delete and re-insert a key on each access and evict map.keys().next().value.
class LRUCache {
constructor(capacity) { this.capacity = capacity; this.map = new Map(); }
get(key) {
if (!this.map.has(key)) return -1;
const value = this.map.get(key);
this.map.delete(key); this.map.set(key, value); // mark as most recent
return value;
}
put(key, value) {
this.map.delete(key);
this.map.set(key, value);
if (this.map.size > this.capacity) this.map.delete(this.map.keys().next().value); // evict oldest
}
}LRU cache — live demo
// Array state
// Pseudocode
How it works
LRU — the data structure
Combine a doubly-linked list (for O(1) move-to-front and remove-tail) with a hash map (for O(1) node lookup by key). The list maintains recency order: head = most recent, tail = least recent.
LRU pseudocode
1// LRU: evict the Least Recently Used item2structure LRUCache(capacity):3 map = HashMap() // key → node4 dll = DoublyLinkedList // MRU at head, LRU at tail56function get(key):7 if key not in map: return -18 node = map[key]9 moveToFront(dll, node) // now most-recently-used10 return node.value1112function put(key, value):13 if key in map:14 map[key].value = value15 moveToFront(dll, map[key])16 else:17 if size == capacity:18 evict = removeTail(dll) // remove LRU19 delete map[evict.key]20 node = new Node(key, value)21 insertHead(dll, node)22 map[key] = node
LFU pseudocode
1// LFU: evict the Least Frequently Used item2structure LFUCache(capacity):3 keyMap = HashMap() // key → (value, freq)4 freqMap = HashMap() // freq → LinkedHashSet(keys)5 minFreq = 167function get(key):8 if key not in keyMap: return -19 updateFreq(key) // increment frequency10 return keyMap[key].value1112function put(key, value):13 if capacity == 0: return14 if key in keyMap:15 keyMap[key].value = value16 updateFreq(key)17 else:18 if size == capacity:19 evict key from freqMap[minFreq]20 insert key with freq=121 minFreq = 1
Complexity — both O(1)
Constant time
Constant time
Constant time
Constant time
Linear time
🎯 Quick Check
In an LRU cache of capacity 3 with items [A, B, C] (A=MRU, C=LRU), you call get(C). What is the new order?
LRU and LFU Caches: summary
Key points
- LRU suits most caches; LFU (Least Frequently Used) suits workloads with stable "hot" items.
- LFU needs frequency counts plus per-frequency lists to stay O(1).
- Browsers, CDNs and databases all use LRU-like eviction.
Frequently asked questions
›What is the difference between LRU and LFU?
LRU evicts the item unused for the longest time. LFU evicts the item used the fewest times overall.