Constrain the number of occurrences in an array

In reply to dave_59:

Thanks Dave! Trust me, i write better code than the pseudo code snippet above :)