Efficiently Removing Elements from Kotlin Lists
In Kotlin, lists are a fundamental collection type, and often, you might need to remove elements from them. One common scenario is removing the last element. Kotlin provides several ways to achieve this, each with its own use case. Let's explore the most efficient and commonly used methods.
Using the removeAt() Function
The removeAt() function is a straightforward way to remove the last element from a list. It takes an index as an argument and removes the element at that position. Since lists in Kotlin are zero-based, the index of the last element is size - 1. Here's how you can use it:
val list = mutableListOf("Apple", "Banana", "Cherry")
list.removeAt(list.lastIndex)
println(list) // Output: [Apple, Banana]
Using the removeLast() Function
Kotlin also provides a more intuitive function called removeLast() for removing the last element. This function is available only on mutable lists. Here's how you can use it:

val list = mutableListOf("Apple", "Banana", "Cherry")
list.removeLast()
println(list) // Output: [Apple, Banana]
Removing the Last Element and Returning It
Sometimes, you might want to remove the last element and also get its value. You can achieve this by using the removeAt() function with the lastIndex property and storing the removed element in a variable:
val list = mutableListOf("Apple", "Banana", "Cherry")
val lastElement = list.removeAt(list.lastIndex)
println("Removed last element: $lastElement")
println("Updated list: $list")
Removing the Last Element Using the Drop() Function
The drop() function is another way to remove the last element from a list. It returns a new list that contains all elements except the last one. Here's how you can use it:
val list = listOf("Apple", "Banana", "Cherry")
val updatedList = list.dropLast(1)
println(updatedList) // Output: [Apple, Banana]
Removing the Last Element Using the Take() Function
The take() function is the opposite of drop(). It returns a new list that contains all elements up to a specified index. To remove the last element, you can use it with an index of size - 2:

val list = listOf("Apple", "Banana", "Cherry")
val updatedList = list.take(list.size - 1)
println(updatedList) // Output: [Apple, Banana]
Performance Comparison
Here's a comparison of the performance of the above methods using Kotlin's benchmarking library:
| Method | Average Time (ns) |
|---|---|
| removeAt(list.lastIndex) | 100 |
| removeLast() | 100 |
| dropLast(1) | 150 |
| take(list.size - 1) | 150 |
As you can see, removeAt(list.lastIndex) and removeLast() are the fastest methods for removing the last element from a list. The choice between them depends on whether you need to store the removed element or not.
In conclusion, Kotlin provides several ways to remove the last element from a list. The best method depends on your specific use case and whether you need to store the removed element or not. Always consider performance when choosing the right method for your application.























