Generate a congruent list with the smallest sum Two lists A and B are congruent if they have the same length, and elements that compare equal in A compare equal in B.

In other words, given any two valid indices x and y:


If A[x] = A[y], then B[x] = B[y].
If A[x] != A[y], then B[x] != B[y].


For example, the lists [1, 2, 1, 4, 5] and [0, 1, 0, 2, 3] are congruent.

The Task

Given a nonempty list of nonnegative integers A, return a new list of nonnegative integers B such that is congruent to A, while minimizing the sum of the integers in B.

There are potentially many possible valid outputs. For example, in the list [12, 700, 3], any permutation of [0, 1, 2] would be considered valid output.

Test Cases

Format:
input -&gt;
one possible valid output

[1 2 1 4 5] -&gt;
[0 1 0 2 3] (this is the example given above)

[3 2 2 1 5 7 2] -&gt;
[1 0 0 2 3 4 0]

[8 8 8 8 8] -&gt;
[0 0 0 0 0]

[2] -&gt;
[0]

[8 6 8 4 6 8 2 4 6 8 0 2 4 6 8] -&gt;
[0 1 0 2 1 0 3 2 1 0 4 3 2 1 0]

[14 1] -&gt;
[1 0]

[19 6 4 9 14 17 10 9 6 14 8 14 6 15] -&gt;
[8 0 3 2 1 7 5 2 0 1 4 1 0 6]

[15] -&gt;
[0]

[1 18 4 8 6 19 12 17 6 13 7 6 8 1 6] -&gt;
[1 8 3 2 0 9 5 7 0 6 4 0 2 1 0]

[9 10 11 9 7 11 16 17 11 8 7] -&gt;
[2 4 0 2 1 0 5 6 0 3 1]

[1 3 16 19 14] -&gt;
[0 1 3 4 2]

[18 8] -&gt;
[1 0]

[13 4 9 6] -&gt;
[3 0 2 1]

[16 16 18 6 12 10 4 6] -&gt;
[1 1 5 0 4 3 2 0]

[11 18] -&gt;
[0 1]

[14 18 18 11 9 8 13 3 3 4] -&gt;
[7 1 1 5 4 3 6 0 0 2]

[20 19 1 1 13] -&gt;
[3 2 0 0 1]

[12] -&gt;
[0]

[1 14 20 4 18 15 19] -&gt;
[0 2 6 1 4 3 5]

[13 18 20] -&gt;
[0 1 2]

[9 1 12 2] -&gt;
[2 0 3 1]

[15 11 2 9 10 19 17 10 19 11 16 5 13 2] -&gt;
[7 2 0 5 1 3 9 1 3 2 8 4 6 0]

[5 4 2 2 19 14 18 11 3 12 20 14 2 19 7] -&gt;
[5 4 0 0 2 1 9 7 3 8 10 1 0 2 6]

[9 11 13 13 13 12 17 8 4] -&gt;
[3 4 0 0 0 5 6 2 1]

[10 14 16 17 7 4 3] -&gt;
[3 4 5 6 2 1 0]

[2 4 8 7 8 19 16 11 10 19 4 7 8] -&gt;
[4 1 0 2 0 3 7 6 5 3 1 2 0]

[15 17 20 18 20 13 6 10 4 19 9 15 18 17 5] -&gt;
[0 1 3 2 3 9 6 8 4 10 7 0 2 1 5]

[15 14 4 5 5 5 3 3 19 12 4] -&gt;
[5 4 2 0 0 0 1 1 6 3 2]

[7 12] -&gt;
[0 1]

[18 5 18 2 5 20 8 8] -&gt;
[2 0 2 3 0 4 1 1]

[4 6 10 7 3 1] -&gt;
[2 3 5 4 1 0]

[5] -&gt;
[0]

[6 12 14 18] -&gt;
[0 1 2 3]

[7 15 13 3 4 7 20] -&gt;
[0 4 3 1 2 0 5]

[10 15 19 14] -&gt;
[0 2 3 1]

[14] -&gt;
[0]

[19 10 20 12 17 3 6 16] -&gt;
[6 2 7 3 5 0 1 4]

[9 4 7 18 18 15 3] -&gt;
[4 2 3 0 0 5 1]

[7 4 13 7] -&gt;
[0 1 2 0]

[19 1 10 3 1] -&gt;
[3 0 2 1 0]

[8 14 20 4] -&gt;
[1 2 3 0]

[17 20 18 11 1 15 7 2] -&gt;
[5 7 6 3 0 4 2 1]

[11 4 3 17] -&gt;
[2 1 0 3]

[1 9 15 1 20 8 6] -&gt;
[0 3 4 0 5 2 1]

[16 13 10] -&gt;
[2 1 0]

[17 20 20 12 19 10 19 7 8 5 12 19] -&gt;
[7 2 2 1 0 6 0 4 5 3 1 0]

[18 11] -&gt;
[1 0]

[2 16 7 12 10 18 4 14 14 7 15 4 8 3 14] -&gt;
[3 9 2 7 6 10 1 0 0 2 8 1 5 4 0]

[5 7 2 2 16 14 7 7 18 19 16] -&gt;
[3 0 1 1 2 4 0 0 5 6 2]

[8 6 17 5 10 2 14] -&gt;
[3 2 6 1 4 0 5]


This is code-golf, so the shortest valid submission (counted in bytes) wins.
Python 2, 62 54 bytes



lambda L:map(sorted(set(L),key=L.count)[::-1].index,L)


Try it online!

Edit: saved 8 bytes via map thanx to Maltysen