Remove an element at given position from an array in Kotlin
This article explores different ways to remove an element at a given position from an integer array in Kotlin.
Unlike a MutableList, arrays in Kotlin are fixed-length. Therefore, an element simply cannot be removed from its position in a Kotlin array. However, there are few alternatives to accomplish this.
1. Using MutableList
The idea is to convert the array into a MutableList and then call the removeAt() function to remove the element present in the specified position. Finally, make a call to toTypedArray() or toIntArray() function to convert the collection back into an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun remove(arr: IntArray, index: Int): IntArray { if (index < 0 || index >= arr.size) { return arr } val result = arr.toMutableList() result.removeAt(index) return result.toIntArray() } fun main() { var arr: IntArray = intArrayOf(1, 2, 3, 4, 5) val index = 2 arr = remove(arr, index) println(arr.contentToString()) // [1, 2, 4, 5] } |
2. Using filter() with map() function
Here, the idea is to get a list of valid indices for the array, excluding the specified index from where the element needs to be removed. Then transform the indices into the corresponding element in the original array and return the collection as an array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun remove(arr: IntArray, index: Int): IntArray { return if (index < 0 || index >= arr.size) { arr } else (arr.indices) .filter { i: Int -> i != index } .map { i: Int -> arr[i] } .toIntArray() } fun main() { var arr: IntArray = intArrayOf(1, 2, 3, 4, 5) val index = 2 arr = remove(arr, index) println(arr.contentToString()) // [1, 2, 4, 5] } |
3. Using System.arraycopy() function
Another idea is to create a new array having one less element than the original array. Then call the System.arraycopy() function to copy elements from the original array into the new array, with the specified element excluded.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
fun remove(arr: IntArray, index: Int): IntArray { if (index < 0 || index >= arr.size) { return arr } val result = IntArray(arr.size - 1) System.arraycopy(arr, 0, result, 0, index) System.arraycopy(arr, index + 1, result, index, arr.size - index - 1) return result } fun main() { var arr: IntArray = intArrayOf(1, 2, 3, 4, 5) val index = 2 arr = remove(arr, index) println(arr.contentToString()) // [1, 2, 4, 5] } |
4. Using for loop
Instead of using System.arraycopy(), we can write our custom routine using Kotlin’s native for-loop. We can do this by creating a new array with one less element and copying the corresponding values from the original array to the new array using a for-loop.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
fun remove(a: IntArray, index: Int): IntArray { if (index < 0 || index >= a.size) { return a } val result = IntArray(a.size - 1) for (i in 0 until index) { result[i] = a[i] } for (i in index until a.size - 1) { result[i] = a[i + 1] } return result } fun main() { var arr: IntArray = intArrayOf(1, 2, 3, 4, 5) val index = 2 arr = remove(arr, index) println(arr.contentToString()) // [1, 2, 4, 5] } |
That’s all about removing an element at a specific index from an array in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)