ChatGPT解决这个技术问题 Extra ChatGPT

Kotlin - Idiomatic way to remove duplicate strings from array?

How to remove duplicates from an Array<String?> in kotlin?

If someone is looking for consecutive characters to remove then visit handyopinion.com/…

C
Community

Use the distinct extension function:

val a = arrayOf("a", "a", "b", "c", "c")
val b = a.distinct() // ["a", "b", "c"]

There's also distinctBy function that allows one to specify how to distinguish the items:

val a = listOf("a", "b", "ab", "ba", "abc")
val b = a.distinctBy { it.length } // ["a", "ab", "abc"]

As @mfulton26 suggested, you can also use toSet, toMutableSet and, if you don't need the original ordering to be preserved, toHashSet. These functions produce a Set instead of a List and should be a little bit more efficient than distinct.

You may find useful:

Kotlin idioms

What Java 8 Stream.collect equivalents are available in the standard Kotlin library?


You can also use toSet or toMutableSet which have less overhead than distinct and if ordering does not matter you can use toHashSet.
@Buckstabue if you only need a Collection back (and it doesn't matter if it is a List or a Set) then using a Collection optimized for unique elements will be more efficient. The current implementation of distinct uses toMutableSet() in its implementation and then converts it to a List so by using toSet et. al. directly you avoid the extra intermediary Collection instance (kotlin/_Arrays.kt:9145-9155 at master · JetBrains/kotlin).
@Buckstabue I see, I believe we're talking about two different issues: 1) to*Set is more efficient (space & time) than distinct[By] because it returns the Set directly instead of using a Set internally and converting it to a List as its return value and 2) distinctBy is can be more efficient than distinct simply because you can avoid full object equality comparison. Both are valid points. I ran with your statement that "certainly it doesn't always have overhead" and I was replying to that and overlooked that you were comparing distinct with distinctBy (and not with to*Set).
@mfulton26, you are correct. I mostly meant that sometimes it's better to use List + distinctBy than Set, because Set intensively use equals/hashCode which potentially might be expensive to call
At time of writing, Iterable.distinct actually does toMutableSet().toList() internally. So don't worry about performance :-)