Skip to main content

How to remove elements from an array in Kotlin

How to remove elements from an array in Kotlin.

Here's a step-by-step tutorial on how to remove elements from an array in Kotlin.

Step 1: Initialize an Array

First, let's initialize an array with some elements. You can do this by using the arrayOf() function and passing the elements as arguments. For example:

val array = arrayOf(1, 2, 3, 4, 5)

In this case, we have created an array with elements 1, 2, 3, 4, 5.

Step 2: Remove an Element by Value

To remove an element from the array based on its value, you can use the filter() function in combination with the toTypedArray() function. Here's an example:

val newArray = array.filter { it != 3 }.toTypedArray()

In this example, we are removing the element 3 from the array. The filter() function takes a lambda expression as an argument and returns a new list containing only the elements that satisfy the condition. Finally, we use the toTypedArray() function to convert the filtered list back into an array.

Step 3: Remove an Element by Index

To remove an element from the array based on its index, you can use the filterIndexed() function in combination with the toTypedArray() function. Here's an example:

val newArray = array.filterIndexed { index, _ -> index != 2 }.toTypedArray()

In this example, we are removing the element at index 2 from the array. The filterIndexed() function takes a lambda expression with two parameters: the index and the element itself. We can use the index parameter to filter out the element we want to remove. Finally, we convert the filtered list back into an array using the toTypedArray() function.

Step 4: Remove Multiple Elements by Value

To remove multiple elements from the array based on their values, you can use the filterNot() function in combination with the toTypedArray() function. Here's an example:

val newArray = array.filterNot { it in listOf(2, 4) }.toTypedArray()

In this example, we are removing the elements 2 and 4 from the array. The filterNot() function takes a lambda expression as an argument and returns a new list containing only the elements that do not satisfy the condition. We use the in operator to check if an element is present in the list of values we want to remove. Finally, we convert the filtered list back into an array using the toTypedArray() function.

Step 5: Remove Multiple Elements by Index

To remove multiple elements from the array based on their indices, you can use the filterIndexed() function in combination with the toTypedArray() function. Here's an example:

val newArray = array.filterIndexed { index, _ -> index !in listOf(1, 3) }.toTypedArray()

In this example, we are removing the elements at indices 1 and 3 from the array. The filterIndexed() function takes a lambda expression with two parameters: the index and the element itself. We can use the index parameter to filter out the elements we want to remove. We use the !in operator to check if an index is not present in the list of indices we want to remove. Finally, we convert the filtered list back into an array using the toTypedArray() function.

Step 6: Verify the Updated Array

To verify that the elements have been successfully removed from the array, you can simply print the contents of the new array. For example:

println(newArray.joinToString())

This will print the elements of the updated array separated by commas.

That's it! You have now learned how to remove elements from an array in Kotlin.