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 purpose of Unit-returning in functions? Why is VALUE there? What is this V ALUE ?

senior

Why is there no static keyword in Kotlin?

middle

What is the Kotlin double-bang !! operator?

Bình luận

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

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