I was wondering if there is a simple or known way to minimize the number of unique elements in a decision variable (vector). Note that I'm not asking for minimization of nonzero elements (rank constraint). In particular I'm searching for a penalization (soft constraint) or hard constraint in the form 
$$f(x) <= n\_{max}$$ 
where $f()$ is what I'm searching for, x is the decision vector and n\_max is the maximum number of unique elements that x is allowed to have. Thank you for your time, Lorenzo



minimize number of unique elements in a vector
While $f(x)=\sum\_i \sum\_j (x\_i-x\_j)^2$ is not quite what you might want, as it would favour vectors with small discrepancy in the entries, it's at least a very nice convex function to minimise, and could be very efficient for certain sets of feasible data.
On the other hand if the entries of your $x$ are only, say, 0-1, then $f$ is exactly what you need.


