Number of FIFO cache misses This challenge is really simple (and a precursor to a more difficult one!).

Given an array of resource accesses (simply denoted by nonnegative integers) and a parameter n, return the number of cache misses it would have assuming our cache has capacity n and uses a first-in-first-out (FIFO) ejection scheme when it is full.

Example:

4, [0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 0, 1, 2, 3]
0 = not in cache (miss), insert, cache is now [0]
1 = not in cache (miss), insert, cache is now [0, 1]
2 = not in cache (miss), insert, cache is now [0, 1, 2]
3 = not in cache (miss), insert, cache is now [0, 1, 2, 3]
0 = in cache (hit), cache unchanged
1 = in cache (hit), cache unchanged
2 = in cache (hit), cache unchanged
3 = in cache (hit), cache unchanged
4 = not in cache (miss), insert and eject oldest, cache is now [1, 2, 3, 4]
0 = not in cache (miss), insert and eject oldest, cache is now [2, 3, 4, 0]
0 = in cache (hit), cache unchanged
1 = not in cache (miss), insert and eject oldest, cache is now [3, 4, 0, 1]
2 = not in cache (miss), insert and eject oldest, cache is now [4, 0, 1, 2]
3 = not in cache (miss), insert and eject oldest, cache is now [0, 1, 2, 3]


So in this example there were 9 misses. Maybe a code example helps explain it better. In Python:

def num_misses(n, arr):
    misses = 0
    cache = []
    for access in arr:
        if access not in cache:
            misses += 1
            cache.append(access)
            if len(cache) &gt; n:
                cache.pop(0)
    return misses


Some further testcases (which contains a hint towards the next challenge - notice anything curious?):

0, [] -&gt; 0
0, [1, 2, 3, 4, 1, 2, 3, 4] -&gt; 8
2, [0, 0, 0, 0, 0, 0, 0] -&gt; 1
3, [3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4] -&gt; 9
4, [3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4] -&gt; 10


Shortest code in bytes wins.
JavaScript (ES6), 55 bytes
Method #1: the cache overwrites the input
Takes input in currying syntax (cache_size)(list).

n=&gt;a=&gt;a.map(x=&gt;a[a.indexOf(x,k&gt;n&amp;&amp;k-n)&lt;k||k++]=x,k=0)|k

Try it online!
How?
We overwrite the input array a[ ] with the cache, using a separate pointer k initialized to 0.
We use a.indexOf(x, k &gt; n &amp;&amp; k - n) &lt; k to test whether x is in the cache.
The cache can't grow faster than the original array is walked through, so each value is guaranteed to be found, either within or beyond the cache window (i.e. indexOf() will never return -1).
A value is in the cache if it is found at an index between max(0, k - n) and k - 1 (both bounds included), in which case we do a[true] = x. This only affects a property of the underlying object behind a[ ] but does not alter the array a[ ]. Otherwise, we do a[k++] = x.
Example
Below are the different steps for the input [1, 1, 2, 3, 3, 2, 1, 4] with a cache size of 2:

bold borders: map() pointer
brackets: cache pointer k
orange: current cache window
yellow: expired cache values



JavaScript (ES6), 57 bytes
Method #2: the cache is appended at the end of the input
Takes input in currying syntax (cache_size)(list).
n=&gt;a=&gt;a.map(x=&gt;n*~a.indexOf(~x,-n)||a.push(~x)&amp;k++,k=0)|k

Try it online!
How?
Because the input array a[ ] is guaranteed to contain non-negative integers, we can safely append the cache at the end of a[ ] by using the one-complement ~x of each value x.
We use n * ~a.indexOf(~x, -n) to test whether ~x is found among the last n values. Whenever this test fails, we append ~x to a[ ] and increment the number of misses k.
Example
Below are the different steps for the same example as above, using this method. Because cache values are simply appended at the end of the array, there is no explicit cache pointer.