This article explores different ways to find the index of an element in an array in Kotlin.

1. Using arrayOf() function

In Kotlin, you can use the indexOf() function that returns the index of the first occurrence of the given element, or -1 if the array does not contain the element.

Download Code

2. Using firstOrNull() function

Another solution is to use the firstOrNull() function to return the index of the first occurrence of the element or null when an element is not found.

Download Code

Another possible way is to perform a linear search on the array to check if the target element is present in the array or not.

Download Code

That’s all about finding the index of an element in an array in Kotlin.