Implement LFU using LRU

๐Ÿ“Œ Core Idea LFU (Least Frequently Used) means: Remove the least frequently used key If multiple keys have same frequency โ†’ remove least recently used (LRU) ๐Ÿ“ฆ Data Structures Used We maintain: key...

By · · 2 min read
Implement LFU using LRU

Source: DEV Community

๐Ÿ“Œ Core Idea LFU (Least Frequently Used) means: Remove the least frequently used key If multiple keys have same frequency โ†’ remove least recently used (LRU) ๐Ÿ“ฆ Data Structures Used We maintain: key โ†’ node (for O(1) access) freq โ†’ doubly linked list (group nodes by frequency) minFreq (track minimum frequency in cache) ๐Ÿงฑ Structure Visualization Freq = 1 โ†’ [ most recent .... least recent ] Freq = 2 โ†’ [ most recent .... least recent ] Freq = 3 โ†’ [ most recent .... least recent ] Each frequency has its own LRU list ๐Ÿš€ Example Walkthrough Capacity = 2 Step 1: put(1,10) Freq 1: [1] minFreq = 1 Step 2: put(2,20) Freq 1: 2, 1 minFreq = 1 Step 3: get(1) Access key 1 Increase its frequency: 1 โ†’ 2 Freq 1: [2] Freq 2: [1] minFreq = 1 ๐Ÿ‘‰ We removed 1 from freq 1 and added to freq 2 Step 4: put(3,30) Cache is FULL โ†’ eviction needed minFreq = 1 Look at Freq 1 โ†’ [2] ๐Ÿ‘‰ Remove 2 After insertion: Freq 1: [3] Freq 2: [1] minFreq = 1 Step 5: get(3) Move 3 from freq 1 โ†’ freq 2 Freq 1: [] Freq 2: [3, 1] min

Related Posts

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#ai (3488)#news (2414)#webdev (1506)#business (1135)#programming (1030)#opensource (846)#/business (829)#security (786)#productivity (735)#sa transcripts (727)

Around the Network