MATLAB: Is it possible to sort an array by making the repeated elements in the last

MATLABrepeated elementssorting

Greetings.
Consider a vector A = [-2 -2 -1 3 3], is it possible to sort this vector (a generalized method) to [-1 -2 -2 -3 -3]?
The only thing that matters is that the repeated elements are in the last, it doesn't matter what way the repeated elements themselves are sorted (for example, [-1 -2 -2 -3 -3] or [-1 -3 -3 -2 -2] are fine) I just want the distinct value to be on the left before any repeated value.
Thank you in advance,
M. Ayoub

Best Answer

>> A = [-2,-2,-1,3,3,3,9,9,0,99]
A =
-2 -2 -1 3 3 3 9 9 0 99
>> [cnt,idx] = histc(A,unique(A));
>> [~,idy] = sort(cnt>1);
>> [~,idz] = ismember(idx,idy);
>> [~,ids] = sort(idz);
>> B = A(ids)
B =
-1 0 99 -2 -2 3 3 3 9 9
And
>> A = [-1,-1,-3,-5,-5]
...
B =
-3 -5 -5 -1 -1