This article explores different ways to remove the first item from an array in Kotlin.

There is no standard solution to remove items from the fixed-length arrays in Kotlin. However, we can create a new array containing all items from the original array except the first. There are several ways to achieve that in Kotlin:

1. Using copyOfRange() function

A common and concise solution to remove the first item from an array is using the copyOfRange() function. It can easily copy the desired range of elements from the original array into a new array.

Download

Output:

[3, 4, 8, 7, 1]

2. Using Range

Another option is to generate a range of indices between 1 and lastIndex (both inclusive), transform the indices into their corresponding elements with the map() function, and return an array of Int containing all the mapped elements.

Download

Output:

[3, 4, 8, 7, 1]

3. Using System.arraycopy() function

Finally, we can allocate a new array of one less size than the original array and then call the System.arraycopy() function to copy the specified range from source to destination array.

Download

Output:

[3, 4, 8, 7, 1]

That’s all about removing the first item from an array in Kotlin.