Index of element in Kotlin

Find this useful? Support us: Star on GitHub 6
Category: Array | Language: Kotlin

To get the index of an element in an array in Kotlin, you can use the indexOf() method.

Here's an example:

val numbers = arrayOf(1, 2, 3, 4, 5)
val index = numbers.indexOf(3)
println("Index of element 3 is $index")

Output:

Index of element 3 is 2

In this example, we first declare an array of numbers. We then use the indexOf() method to find the index of the element 3 in the numbers array. Finally, we print the result using the println() function.