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

middle

What is the difference between suspending vs. blocking?

senior

What are Object expressions in Kotlin and when to use them?

expert

What is The Billion Dollar Mistake?

Bình luận

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

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