1. Introduction

In Kotlin, working with arrays is a common task in many programming scenarios. One frequently encountered task is finding the index of a specific element within an array. Whether a beginner or an experienced Kotlin developer, understanding how to efficiently locate elements in an array can be incredibly useful. In this tutorial, we’ll explore various methods to find the index of an element in a Kotlin Array, along with code examples to illustrate each approach.

2. The indexOf() Function

The indexOf() function is the simplest way to find the index of an element in a Kotlin Array. It returns the index of the first occurrence of the specified element, or -1 if the element is not found:

@Test
fun `Find index using indexOf`() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val elementToFind = 3

    val index = numbers.indexOf(elementToFind)

    assertEquals(2, index)
}

3. The indexOfFirst() Function

The indexOfFirst() function allows us to find the index of the first element that satisfies a given predicate, and it returns -1 if no such element is found:

@Test
fun `Find index of first`() {
    val numbers = arrayOf(1, 2, 3, 4, 3, 5)
    val elementToFind = 3

    val index = numbers.indexOfFirst { it == elementToFind }

    assertEquals(2, index)
}

4. The lastIndexOf() Function

Similar to indexOfFirst(), we have lastIndexOf(). It returns the last index of an element, or -1 if the element is not found:

@Test
fun `Find last index of element`() {
    val numbers = arrayOf(1, 2, 3, 4, 3, 5)
    val elementToFind = 3

    val lastIndex = numbers.lastIndexOf(elementToFind)

    assertEquals(4, lastIndex)
}

5. The indexOfLast() Function

The indexOfLast() function allows us to find the last element that satisfies a given predicate, similarly to how indexOfFirst() does. It returns the last index of an element matching the given predicate, or -1 if not found:

@Test
fun `Find index of last`() {
    val numbers = arrayOf(1, 2, 3, 4, 3, 5)
    val elementToFind = 3

    val index = numbers.indexOfLast { it == elementToFind }

    assertEquals(4, index)
}

6. Using a Loop

We can also find the index of an element by iterating through the Array using a loop. This approach is useful when we need to find the indexes of all occurrences of an element:

@Test
fun `Find indices using loop`() {
    val numbers = arrayOf(1, 2, 3, 4, 3, 5)
    val elementToFind = 3

    val indices = mutableListOf<Int>()

    for (i in numbers.indices) {
        if (numbers[i] == elementToFind) {
            indices.add(i)
        }
    }

    assertEquals(listOf(2, 4), indices)
}

7. Conclusion

Finding the index of an element in a Kotlin Array is a common task, and we’ve explored several ways to accomplish this.

Depending on our specific use case, we can choose the method that best suits our needs. We should always remember to handle cases where the element may not exist in the Array to ensure our code is robust and error-resistant.

As always, the code used in this article is available over on GitHub.