Arun M
04/28/2023, 9:25 AMSam
04/28/2023, 9:26 AMSam
04/28/2023, 9:27 AMCLOVIS
04/28/2023, 9:47 AMlist.sort() // mutates
list.sorted() // immutable copyCLOVIS
04/28/2023, 9:49 AMList. The efficient data structure to avoid duplicates is a Set .
.disctint() actually creates a Set , adds all elements to it (thus removing duplicates), then creates a new List and adds back all elements to it. Depending on what you want to do, it may be a better idea to use a Set from the start and completely avoid List hereKlitos Kyriacou
04/28/2023, 10:49 AMsorted are past participle adjectives. Names like distinct are just plain vanilla adjectives.CLOVIS
04/28/2023, 11:09 AMephemient
04/28/2023, 3:43 PMcharList.retainAll(mutableSetOf<Char>()::add)
will remove all duplicatesCLOVIS
04/28/2023, 3:45 PMephemient
04/28/2023, 3:46 PMval set = mutableSetOf<Char>()
charList.retainAll { set.add(it) }
which is equivalent to
val set = mutableSetOf<Char>()
val iterator = charList.iterator()
while (iterator.hasNext()) {
if (!set.add(iterator.next() {
iterator.remove()
}
}