I am programming in Kotlin and have a MutableList from which I would like to remove the first n
elements from that specific list instance. This means that functions like MutableList.drop(n)
are out of the question.
One solution would of course be to loop and call MutableList.removeFirst()
n
times, but this feels inefficient, being O(n
). Another way would be to choose another data type, but I would prefer not to clutter my project by implementing my own data type for this, if I can avoid it.
Is there a faster way to do this with a MutableList? If not, is there another built-in data type that can achieve this in less than O(n
)?
One method which seems to be faster if
n
is sufficiently large seems to be the following:listSize - n
bytes to keep in a temporary list,Here is a quick benchmark for some example values that happen to fit my use case:
Output: