Kotlin Array.lastIndexOf() function
In Kotlin, the Array.lastIndexOf()
function is used to obtain the last index of the specified element in the array. If the specified element is not found in the array, the function returns -1. You can also provide an optional starting index for the search.
This function is useful when you need to find the last occurrence of an element in an array or determine if a particular element exists in the array.
In this tutorial, we’ll explore the syntax of the lastIndexOf()
function and provide examples of its usage in Kotlin arrays.
Syntax
The syntax of the lastIndexOf()
function is as follows:
fun <T> Array<out T>.lastIndexOf(
element: T
): Int
where
Parameter | Description |
---|---|
element | The element to search for in the array. |
Examples for Array.lastIndexOf() function
1. Finding the Last Index of an Element in given Array
In this example, we’ll use lastIndexOf()
to find the last index of the element 3
in the give array numbersArray
.
Kotlin Program
fun main() {
val numbersArray = arrayOf(1, 2, 3, 4, 5, 3, 6, 7, 8, 9, 3)
// Finding the last index of the element 3
val lastIndex = numbersArray.lastIndexOf(3)
// Printing the original array and the result
println("Numbers Array\n${numbersArray.joinToString(", ")}\n")
println("Last Index of 3\n$lastIndex")
}
Output
Numbers Array
1, 2, 3, 4, 5, 3, 6, 7, 8, 9, 3
Last Index of 3
10
2. Finding the Last Index of a String value in a String Array
In this example, we’ll take a string array, and use lastIndexOf()
to find the last index of "apple"
string value.
Kotlin Program
fun main() {
val strArray = arrayOf("apple", "banana", "apple", "apple", "fig")
val searchElement = "apple"
// Finding the last index of the element "apple"
val lastIndex = strArray.lastIndexOf(searchElement)
// Printing the original array and the result
println("Numbers Array\n${strArray.contentToString()}\n")
println("Last Index of ${searchElement}\n$lastIndex")
}
Output
Numbers Array
[apple, banana, apple, apple, fig]
Last Index of apple
3
Explanation
[apple, banana, apple, apple, fig] ← array
0 1 2 3 4 ← indices
↑
last index
3. When Search Element is not present in Array
In this example, we’ll take a string array, and use lastIndexOf()
to find the last index of "mango"
string value. Since, the search element is not present in the array, lastIndexOf()
function should return -1
.
Kotlin Program
fun main() {
val strArray = arrayOf("apple", "banana", "apple", "apple", "fig")
val searchElement = "mango"
// Finding the last index of the element "apple"
val lastIndex = strArray.lastIndexOf(searchElement)
// Printing the original array and the result
println("Numbers Array\n${strArray.contentToString()}\n")
println("Last Index of ${searchElement}\n$lastIndex")
}
Output
Numbers Array
[apple, banana, apple, apple, fig]
Last Index of mango
-1
Summary
In this tutorial, we’ve covered the lastIndexOf()
function in Kotlin arrays, its syntax, and how to use it to find the last index of a specified element in the Array. We have covered the use cases: 1. When search element is present in the Array, 2. When search element is not present in the Array.