Insert an element at a given position into an array in Kotlin
This article explores different ways to insert an element at a given position into an array in Kotlin.
The arrays in Kotlin are fixed-size. That means once created, we can’t resize them. To get a resizable-array implementation, consider using MutableList instead, which can grow or shrink automatically as needed.
However, if you insist on using arrays, you can use any of the following methods:
1. Using a new array
A simple solution is to create a new array with one more element to accommodate the additional element and populate it with corresponding values from the original array with the given element placed at its correct position.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
fun insertAt(arr: IntArray, key: Int, index: Int): IntArray { val array = IntArray(arr.size + 1) for (i in 0 until index) { array[i] = arr[i] } array[index] = key for (i in index + 1..arr.size) { array[i] = arr[i - 1] } return array } fun main() { var arr = intArrayOf(4, 7, 5, 2) val key = 3 val index = 2 arr = insertAt(arr, key, index) println(arr.contentToString()) // [4, 7, 3, 5, 2] } |
You can replace the for-loop with System.arraycopy() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun insertAt(arr: IntArray, key: Int, index: Int): IntArray { val result = IntArray(arr.size + 1) System.arraycopy(arr, 0, result, 0, index) result[index] = key System.arraycopy(arr, index, result, index + 1, arr.size - index) return result } fun main() { var arr = intArrayOf(4, 7, 5, 2) val key = 3 val index = 2 arr = insertAt(arr, key, index) println(arr.contentToString()) // [4, 7, 3, 5, 2] } |
2. Using MutableList
Another solution is to convert the array into a MutableList, add the given element at its correct position in the list, and finally return an array containing all the elements in this list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun insertAt(arr: IntArray, key: Int, index: Int): IntArray { val result = arr.toMutableList() result.add(index, key) return result.toIntArray(); } fun main() { var arr = intArrayOf(4, 7, 5, 2) val key = 3 val index = 2 arr = insertAt(arr, key, index) println(arr.contentToString()) // [4, 7, 3, 5, 2] } |
3. Using map() function
You can also use the map() function in Kotlin like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun insertAt(arr: IntArray, key: Int, index: Int): IntArray { return (0..arr.size) .map { i: Int -> return@map when { i < index -> arr[i] i == index -> key else -> arr[i - 1] }}.toIntArray() } fun main() { var arr = intArrayOf(4, 7, 5, 2) val key = 3 val index = 2 arr = insertAt(arr, key, index) println(arr.contentToString()) // [4, 7, 3, 5, 2] } |
That’s all about inserting an element at a given position into 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 :)