How to idiomatically remove duplicates from Array<String?> in kotlin?
There are several ways to solve this problem, including;
1). Using distinct extension function :
val a = arrayOf("a", "a", "b", "c", "c")
val b = a.distinct() // ["a", "b", "c"]
There is also a distinctBy function that allows one to specify how to distinguish items:
val a = listOf("a", "b", "ab", "ba", "abc")
val b = a.distinctBy { it.length } // ["a", "ab", "abc"]
You can also use toSet , toMutableSet and if you don't need to preserve the original order, toHashSet . These functions produce a Set , not a List , and should be slightly more efficient than Distinct .