Given an n non-negative integers, how many numbers must be changed so that each number x appears x times? You cannot add or remove elements, only replace them with other numbers.
For example:
1 6 23 6 9 23 1 1 6
can be turned into:
1 6 2 6 6 2 6 6 6
with 5 changes, which is optimal.
Test cases
0 => 1
1 2 3 => 1
5 5 5 5 5 => 0
23 7 4 8 => 3
100 100 100 6 => 4
5 5 5 5 5 5 => 1
1 2 2 4 4 4 4 9 9 9 9 => 5
Shortest code wins!