How to remove specific elements from an array using index in Kotlin

Kotlin program to remove specific elements from an array using index:

In this post, we will learn how to remove specific elements from an array in Kotlin. Arrays are of fixed length in Kotlin and we can’t remove an item from an array or we can’t modify an array. The only way is to convert the array to a MutableList, remove the item and convert it back to an array.

MutableList:

MutableList is a collection of elements and it is mutable, i.e. we can add or remove items from it. It has different methods available to remove elements. Since we want to remove an element by its index, we have to use the removeAt method. This method takes the index as its parameter and removes the item at that index.

removeAt method of MutableList:

The removeAt method of MutableList is defined as like below:

abstract fun removeAt(index: Int): E

It takes the index as its parameter and remotes the item at that specific index. It returns the item removed.

To call the removeAt method, we have to convert the array to a mutable list. There is one method called toMutableList which can be used to convert an array to a mutable list.

toMutableList method:

The toMutableList method is defined as like below:

fun <T> Array<out T>.toMutableList(): MutableList<T>

This method creates a MutableList with the elements of the array and returns it.

So, we have to first convert the array to a MutableList to remove an item at any given index position.

Kotlin program to remove specific elements from an array by using the index:

The below program removes an item by using its index from an array.

fun main() {
    val givenArray = arrayOf("one", "two", "three", "four")

    val mutableList = givenArray.toMutableList()
    mutableList.removeAt(3)
    val finalArray = mutableList.toTypedArray()

    println("Given Array: ${givenArray.contentToString()}")
    println("Final Array: ${finalArray.contentToString()}")
}

Here,

  • givenArray is the original array.
  • We created one mutable list by using the toMutableList method. It is assigned to mutableList variable.
  • The removeAt method is used to remove the item at index 3 in the mutable list mutableList.
  • We are again calling toTypedArray method to convert the mutable list to an array and it is assigned to finalArray variable.
  • The last two lines are printing the given array and the modified array.

If you run this program, it will print:

Kotlin remove specific elements from array

As you can see here, the item at index 3 is removed from the array.

Generic function to handle different types of arrays:

Let’s create a generic function to handle array of different types:

inline fun <reified T> removeItem(array: Array<T>, index: Int): Array<T> {
    val mutableList = array.toMutableList()
    mutableList.removeAt(index)
    return mutableList.toTypedArray()
}

fun main() {
    val strArray = arrayOf("one", "two", "three", "four")
    val intArray = arrayOf(1, 2, 3, 4, 5)
    val charArray = arrayOf('c', 'a', 'b', 'd', 'e')

    println("Given: ${strArray.contentToString()}, Result: ${removeItem(strArray, 2).contentToString()}")
    println("Given: ${intArray.contentToString()}, Result: ${removeItem(intArray, 2).contentToString()}")
    println("Given: ${charArray.contentToString()}, Result: ${removeItem(charArray, 2).contentToString()}")
}

The removeItem method takes the array and the index as its parameters and returns the modified array. To test this method, we created three arrays: strArray, intArray and charArray with string, integer, and character elements.

For all of these arrays, the item at index 2 is removed. It will work with all of these arrays.

Output:

Given: [one, two, three, four], Result: [one, two, four]
Given: [1, 2, 3, 4, 5], Result: [1, 2, 4, 5]
Given: [c, a, b, d, e], Result: [c, a, d, e]

You might also like: