This article explores different ways to remove a slice from a list between two specified indexes in Kotlin.

1. Custom Routine

The idea is to iterate backward in the list and remove all elements within the specified range.

Download Code

 
Note that advancing in a list and removing elements from it might cause it to skip a few elements and give unexpected results.

Download Code

2. Using clear() function

Another good solution is to use the clear() function along with the subList() function to remove a specific range from a list.

Download Code

3. Using removeAll() function

The removeAll() function removes all elements in the list that are contained in the specified collection. You can pass a sublist returned by subList() function to removeAll() function to remove them.

Download Code

 
The slice() function can also be used instead of subList().

Download Code

That’s all about removing a slice from a list in Kotlin.