What is the idiomatic way to remove duplicate strings from array?
What is the idiomatic way to remove duplicate strings from array?
Here is the idiomatic way to remove duplicate strings from an array in Kotlin:
val myArray = arrayOf("apple", "banana", "cherry", "banana", "date")
val uniqueArray = myArray.toSet().toTypedArray()
println(uniqueArray.contentToString())
Output:
[apple, banana, cherry, date]
Here's how it works:
We start with an array of strings called myArray
.
To remove duplicates, we first convert the array to a Set
using toSet()
. A Set
is an unordered collection that only contains unique elements.
Since toSet()
returns a Set<String>
, we ...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào