import numpy as np
a = np.array([2, 3, 2, 2, 3, 3, 2, 1])
uniques, count = np.unique(a, return_counts=True)
np.repeat(uniques, np.clip(count, 0, 2))
numpy select fixed amount of values among duplicate values in array