Câu hỏi phỏng vấn Kotlin
Câu hỏi

What is the idiomatic way to remove duplicate strings from array?

Câu trả lời

Here is the idiomatic way to remove duplicate strings from an array in Kotlin:

kotlin Copy
val myArray = arrayOf("apple", "banana", "cherry", "banana", "date")

val uniqueArray = myArray.toSet().toTypedArray()

println(uniqueArray.contentToString())

Output:

Copy
[apple, banana, cherry, date]

Here's how it works:

  1. We start with an array of strings called myArray.

  2. To remove duplicates, we first convert the array to a Set using toSet(). A Set is an unordered collection that only contains unique elements.

  3. Since toSet() returns a Set<String>, we ...

junior

junior

Gợi ý câu hỏi phỏng vấn

expert

Why do we use companion object as a kind of replacement for Java static fields in Kotlin?

junior

What is a data class in Kotlin?

junior

Where should I use var and where val ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào