title: Program Page Replacement Algorithms Set 2 Fifo
signature: def program_page_replacement_algorithms_set_2_fifo(pages, n, capacity):
test_code: import array
  import bisect
  import collections
  import datetime
  import functools
  import heapq
  import itertools
  import math
  import queue
  import re
  import string
  import sys
  from typing import Any, Dict, List, Optional, Set
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  def validate_solution(actual, expected):
      return actual == expected
  
  def driver(pages, n, capacity, expected):
      try:
          if validate_solution(PLACEHOLDER_FN_NAME(
              pages, n, capacity),
              expected
          ):
              return "PASSED"
          return "FAILED"
      except Exception as exception_obj:
          return type(exception_obj).__name__
      
  if __name__ == "__main__":
      result = driver([4, 4, 6, 7, 8, 11, 13, 18, 26, 35, 36, 37, 45, 46, 46, 47, 48, 49, 51, 52, 53, 56, 61, 74, 75, 77, 80, 83, 85, 86, 87, 90, 93, 95, 97, 98, 99, 99], 36, 37, 34)
      print(f"TEST-0...{result}")
  
      result = driver([78, -48, 50, -20, -6, 58, -8, 66, 72, 68, 4, 80, 58, -26, -82, -56, 92, 76, 20, 82, -46, 86, 38, 60, -62, -48, 76, 8, -66, -4, -98, -96, -52, 92], 33, 23, 30)
      print(f"TEST-1...{result}")
  
      result = driver([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1], 19, 13, 2)
      print(f"TEST-2...{result}")
  
      result = driver([98, 78, 94, 42, 62, 83, 7, 62, 60, 94, 16, 28, 50, 15, 18, 71, 86, 47, 62, 89], 15, 11, 13)
      print(f"TEST-3...{result}")
  
      result = driver([-82, -70, -68, -56, -50, -44, 4, 18, 28, 30, 30, 42, 66, 78, 80], 9, 11, 9)
      print(f"TEST-4...{result}")
  
      result = driver([1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0], 25, 25, 2)
      print(f"TEST-5...{result}")
  
      result = driver([4, 5, 13, 15, 18, 28, 32, 40, 46, 46, 55, 57, 61, 63, 65, 68, 77, 79, 79, 96], 17, 18, 16)
      print(f"TEST-6...{result}")
  
      result = driver([-2, 82, 2, -74, -6, -24, 54, -74, -98, 8, -94, -60, -42, -38, 36, -38, -58, -70, -28, -34, 70, -6, -2, -76, -40, -4, 0, -4, 76, 48, -34, -26, -48, -58, -88, -44, 20, -22, 78], 31, 24, 25)
      print(f"TEST-7...{result}")
  
      result = driver([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 26, 24, 2)
      print(f"TEST-8...{result}")
  
      result = driver([4, 90, 28, 71, 69, 45, 92, 63, 72, 76, 47, 85, 36, 59, 88, 46, 28, 19, 50, 31, 63, 13], 15, 12, 15)
      print(f"TEST-9...{result}")
  
      sys.exit(0)
source_py: def program_page_replacement_algorithms_set_2_fifo(pages, n, capacity):
      s = set()
      indexes = Queue()
      page_faults = 0
      for i in range(n):
          if (len(s) < capacity):
              if (pages[i] not in s):
                  s.add(pages[i])
                  page_faults += 1
                  indexes.put(pages[i])
          elif (pages[i] not in s):
              val = indexes.queue[0]
              indexes.get()
              s.remove(val)
              s.add(pages[i])
              indexes.put(pages[i])
              page_faults += 1
      return page_faults
source_cpp: int program_page_replacement_algorithms_set_2_fifo(vector<int> pages, int n, int capacity) {
    unordered_set < int > s;
    queue < int > indexes;
    int page_faults = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( s . size ( ) < capacity ) {
        if ( s . find ( pages [ i ] ) == s . end ( ) ) {
          s . insert ( pages [ i ] );
          page_faults ++;
          indexes . push ( pages [ i ] );
        }
      }
      else {
        if ( s . find ( pages [ i ] ) == s . end ( ) ) {
          int val = indexes . front ( );
          indexes . pop ( );
          s . erase ( val );
          s . insert ( pages [ i ] );
          indexes . push ( pages [ i ] );
          page_faults ++;
        }
      }
    }
    return page_faults;
  }