Program for Least Recently Used ( LRU ) Page Replacement algorithm | C # program for page replacement algorithms ; Driver method ; To represent set of current pages . We use an Arraylist ; Insert it into set if not present already which represents page fault ; Check if the set can hold equal pages ; Increment page faults ; Remove the indexes page ; insert the current page
using System ; using System . Collections . Generic ; class LRU { public static void Main ( String [ ] args ) { int capacity = 4 ; int [ ] arr = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; List < int > s = new List < int > ( capacity ) ; int count = 0 ; int page_faults = 0 ; foreach ( int i in arr ) { if ( ! s . Contains ( i ) ) { if ( s . Count == capacity ) { s . RemoveAt ( 0 ) ; s . Insert ( capacity - 1 , i ) ; } else s . Insert ( count , i ) ; page_faults ++ ; ++ count ; } else { s . Remove ( i ) ; s . Insert ( s . Count , i ) ; } } Console . WriteLine ( page_faults ) ; } }