This article explores different ways to remove an element from an array in Kotlin.

Arrays hold a fixed number of items of a single type. Since the length of the array is fixed, we cannot remove elements from it. However, we can create a new array without the element being removed.

1. Using filter() function

The idea is to filter the array to remove the required element and accumulate the remaining values into a new array. This is demonstrated below for an Int Array.

Download Code

Output:

[2, 5, 7, 3, 4, 8, 9]

2. Using System.arraycopy() function

To remove an element from the array using its index, the recommended approach is using the System.arraycopy() function. The idea is to allocate a new array of one less size than the original array, and copy all the elements from the original array into the new array before and after that index.

Download Code

Output:

[A, B, C, D, E]

That’s all about removing an element from an array in Kotlin.