I have a List of Objects, and I want to turn it into a distinct list while mapping all indices to the new indices.
Example:
List: ["a", "b", "a", "d"]
-> ["a", "b", "d"]
Map:
{
0: 0, //0th index of original list is now 0th index of distinct list
1: 1,
2: 0, //2nd index of original list is now 0th index of distinct list
3: 2 //3rd index of original list is now 2th index of distinct list
}
Is there a simple way to do this with a one-liner or with a fairly simple solution in kotlin?
The following expression will do that:
val p = listOf("a", "b", "a", "d").let {
val set = it.distinct().mapIndexed { i, v -> v to i }.toMap()
it.mapIndexed { i, v -> i to set.getValue(v) }
}.toMap()