I need to remove excessive duplicates of particular element from the list, that is, if there’s more than max_dup duplicates of selected elements in a slice of the list, leave at most max_dup elements in place. Obviously, stability (retaining order of elements in the list) is a must. E.g.