is there a list structure in Kotlin which keeps di...
# getting-started
d
is there a list structure in Kotlin which keeps distinct values only?
Copy code
e.g.  val list = listOf(3,9,1,6,7)
if I add a new 9, it would removes the old one:
Copy code
list.add(9)
println(list) // listOf(3,1,6,7,9)
c
a set?
val set = setOf(3, 9, 1, 6, 7)
d
Copy code
val set = mutableSetOf(3, 9, 1, 6, 7)
set.add(9)
println(set)
it still prints [3, 9, 1, 6, 7]
I would like it to remove the old one
e
mutableSetOf() preserves insertion order. remove and re-add
or build your own LRU, that's a pretty typical CS data structure
👆 5
d
I was hoping Kotlin already had a structure like that
e
it's not in any language's stdlib that I know of, partly because it's not that universal: all users wants slightly different things out of it
s
yep ^^
Copy code
public fun <T> Iterable<T>.distinct(): List<T> {
    return this.toMutableSet().toList()
}
pretty convenient for a stdlib as it is