kotlin

Get the index of an item in an Array Kotlin

In this post, we are going to explain different methods that can be used to get the index of an element in an array. We use the indexOf() function of Kotlin to get the index of an item.

fun main() {
    val arr = arrayOf<String>("Rusty", "Amit", "Martin", "Luther", "Joy")
    
    val index = arr.indexOf("Luther")
    
    println(index)
}

Output

3

In the above code example, we have created an array named arr that contains multiple String type values in it. We want to get the index of an item named Luther in it. To get the index, we are using the indexOf() function of the Kotlin Arrays.

Syntax

Array.indexOf(item)

Get the index of an item in integer type values array

fun main() {
    val arr = intArrayOf(1, 2, 3, 4, 5, 6)
    
    val index = arr.indexOf(5)
    
    println(index)
}

Output

4

Or you can also create an integer type values array as below and get the index of an element.

fun main() {
    val numbers = arrayOf<Int>(50, 90, 70, 30, 20, 21)
    
    val result = numbers.indexOf(90)
    
    println("Index of 90 is: " + result)
}

Output

Index of 90 is: 1

If the item provided as a parameter in the indexOf() function does not exist in the array then it will return -1.

Check if an item exists in Array using indexOf() function

We can also check if an element exists in an array or not using the indexOf() function in Kotlin. If the function returns -1 then the item does not exist in the array else the item is present in the array.

Code Example

fun main() {
    // create an array
    val names = arrayOf<String>("Tina", "Jyoti", "Roy", "Hemlin")
    
    // check if item - Mark exists in the array
	if (names.indexOf("Mark") == -1) {
        println("Item does not exist in the array")
    } else {
        println("Item exists in the array")
    }
}

Output

Item does not exist in the array

Get item index using firstOrNull() function

We can also use firstOrNull() function to get the index of an item. The function will return the index of the first occurrence of the item in the Array. If the array does not exist in the Array it will return null.

fun main() {
    // create an array
    val cars = arrayOf("Honda", "Tesla", "Mustang", "Ferrari", "Volvo")
    
    // find index of item - Volvo
    val item: String = "Volvo"
    val index = (cars.indices).firstOrNull { i: Int -> item == cars[i] }
    
    println("Volvo index is: " + index)
}

Output

Volvo index is: 4

Use For loop to find the index of an item in Array

If you do not want to use the built-in functions of Kotlin Arrays to find the index of an item in Array, you can use the For Loop to find the index of an element in the array. We can understand it using the below code example. 

fun main() {
    // create an array
    val cars = arrayOf<String>("Honda", "Tesla", "Mustang", "Ferrari", "Volvo")
    
    val item: String = "Mustang"
    var index: Int = -1
    
    // use for loop to get the index of the item in the array
    for (i in cars.indices)
    {
        if (cars[i] == item) {
            index = i
            break;
        }
    }
    
    println("Mustang index is: " + index)
}

Output

Mustang index is: 2
Was this helpful?