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

When would you use Elvis operator in Kotlin?

middle

What is the difference between open and public in Kotlin?

middle

What is a purpose of Companion Objects in Kotlin?

Bình luận

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

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