Skip to main content

How to find the last occurrence of an element in an array in Kotlin

How to find the last occurrence of an element in an array in Kotlin.

Here's a detailed step-by-step tutorial on how to find the last occurrence of an element in an array in Kotlin.

Step 1: Create an array

First, you need to create an array in Kotlin. An array is a collection of elements of the same type. You can define an array using the arrayOf() function, specifying the elements inside the parentheses.

val array = arrayOf(1, 2, 3, 4, 5, 4, 3, 2, 1)

In this example, we have created an array of integers with some duplicate elements.

Step 2: Iterate over the array

To find the last occurrence of an element in the array, you need to iterate over the array elements. There are multiple ways to iterate over an array in Kotlin, such as using a for loop or forEachIndexed() function.

Option 1: Using a for loop You can use a for loop to iterate over the array elements and find the last occurrence of a specific element.

val targetElement = 3
var lastIndex = -1

for (i in array.indices.reversed()) {
if (array[i] == targetElement) {
lastIndex = i
break
}
}

println("Last occurrence of $targetElement is at index $lastIndex")

In this example, we iterate over the array in reverse order using the indices.reversed() property. Inside the loop, we check if the current element is equal to the target element. If it is, we update the lastIndex variable and break out of the loop. Finally, we print the index of the last occurrence of the target element.

Option 2: Using forEachIndexed() function Another way to iterate over the array is by using the forEachIndexed() function. This function allows you to access both the element and its index.

val targetElement = 3
var lastIndex = -1

array.forEachIndexed { index, element ->
if (element == targetElement) {
lastIndex = index
}
}

println("Last occurrence of $targetElement is at index $lastIndex")

In this example, we use the forEachIndexed() function to iterate over the array. Inside the lambda expression, we check if the current element is equal to the target element. If it is, we update the lastIndex variable with the current index.

Step 3: Handle the case when the element is not found

In some cases, the element you are searching for may not exist in the array. To handle this scenario, you can initialize the lastIndex variable with a default value (-1 in this example). If the lastIndex remains unchanged after iterating over the array, it means that the element was not found.

val targetElement = 10
var lastIndex = -1

array.forEachIndexed { index, element ->
if (element == targetElement) {
lastIndex = index
}
}

if (lastIndex == -1) {
println("Element $targetElement not found in the array")
} else {
println("Last occurrence of $targetElement is at index $lastIndex")
}

In this example, we search for the element 10, which does not exist in the array. Therefore, the lastIndex remains -1, and we print a message indicating that the element was not found.

That's it! You have learned how to find the last occurrence of an element in an array in Kotlin. You can use either a for loop or the forEachIndexed() function to iterate over the array and update the index when the target element is found. Remember to handle the case when the element is not found to avoid unexpected behavior.