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 are some disadvantages of Kotlin?

junior

What is basic difference between fold and reduce in Kotlin? When to use which?

expert

What is the difference between * and Any in Kotlin generics?

Bình luận

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

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