Best way to "replace" an element of an immutable list

What would be even faster is to do this:

fun <T> List<T>.update(index: Int, item: T): List<T> = toMutableList().apply { this[index] = item) } 

since it’ll just do one array copy under the hood and then it’ll replace that one element in the array.

1 Like