Skip to main content
St Louis

Back to all posts

How to Remove an Element From an Array<String> In Kotlin?

Published on
6 min read
How to Remove an Element From an Array<String> In Kotlin? image

Best Kotlin Programming Books to Buy in September 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Head First Kotlin: A Brain-Friendly Guide

Head First Kotlin: A Brain-Friendly Guide

BUY & SAVE
$50.36 $79.99
Save 37%
Head First Kotlin: A Brain-Friendly Guide
3 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
4 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
5 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
6 Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

BUY & SAVE
$33.00 $38.99
Save 15%
Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language
7 Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

BUY & SAVE
$50.80
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)
8 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.53
Kotlin: An Illustrated Guide
9 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
+
ONE MORE?

In Kotlin, removing an element from an array of strings involves a few steps:

  1. Create an array of strings: First, you need to create an array of strings. You can do this by declaring and initializing the array using the arrayOf() function. For example:

val array: Array = arrayOf("apple", "banana", "orange", "grape")

  1. Find the index of the element to remove: To remove an element, you need to find its index in the array. You can use the indexOf() function to do this. Pass the element you want to remove as the parameter, and it will return the index of the element. For example, to find the index of "orange":

val index = array.indexOf("orange")

  1. Remove the element: Once you have the index, you can remove the element from the array using the removeAt() function. Pass the index as the parameter to remove the element from the array. For example, to remove the element at index 2 (i.e., "orange"):

array.removeAt(index)

After performing these steps, the element will be removed from the array.

Note: If the element does not exist in the array, the indexOf() function will return -1. It is a good practice to check whether the index is -1 before removing the element to avoid any potential errors.

How can I delete an element from an array in Kotlin?

To delete an element from an array in Kotlin, you can use the removeAt() method or the remove() method. Here is how you can use each method:

  1. Using the removeAt() method:

val numbers = arrayOf(1, 2, 3, 4, 5) val indexToRemove = 2

numbers.removeAt(indexToRemove) // removes element at index 2

// Print the updated array println(numbers.contentToString()) // Output: [1, 2, 4, 5]

  1. Using the remove() method:

val numbers = arrayOf(1, 2, 3, 4, 5) val elementToRemove = 3

numbers.remove(elementToRemove) // removes the first occurrence of element 3

// Print the updated array println(numbers.contentToString()) // Output: [1, 2, 4, 5]

Note: Please be aware that the remove() method returns a Boolean indicating whether the removal was successful or not. Also, both removeAt() and remove() methods modify the original array, so make sure to create a copy before performing these operations if you want to keep the original array intact.

What is the fastest way to remove an element from an array in Kotlin?

One of the fastest ways to remove an element from an array in Kotlin is by using the ArrayList data structure and its associated methods. Here's an example:

val array = arrayListOf(1, 2, 3, 4, 5) val elementToRemove = 3

array.remove(elementToRemove)

In this example, the remove method is used to remove the specified element from the array. If the element is found, it is removed, and if it is not found, the array remains unchanged.

Alternatively, if you want to remove all occurrences of the element, you can use the removeAll method:

val array = arrayListOf(1, 2, 3, 4, 3, 5) val elementToRemove = 3

array.removeAll { it == elementToRemove }

In this case, removeAll removes all elements that match the specified predicate, effectively removing all occurrences of the element from the array.

How to remove all occurrences of an element from an array in Kotlin?

To remove all occurrences of an element from an array in Kotlin, you can use the filterNot() function. Here's an example code:

fun main() { val numbers = arrayOf(1, 2, 3, 4, 3, 5, 3)

val elementToRemove = 3
val filteredArray = numbers.filterNot { it == elementToRemove }

println(filteredArray.joinToString())

}

Output:

1, 2, 4, 5

In the code above, we have an array numbers that contains multiple occurrences of the element 3. To remove all occurrences of 3 from the array, we use the filterNot() function along with a lambda expression. The lambda expression it == elementToRemove checks if the current element is equal to elementToRemove. If it is not equal, then the element is kept in the resulting array filteredArray. Finally, we print the filtered array using joinToString() function.

One recommended way to remove null elements from an array in Kotlin is by using the filterNotNull function.

Here's an example of how you can use filterNotNull to remove null elements from an array:

val array = arrayOf(1, null, 3, null, 5, null)

val filteredArray = array.filterNotNull()

println(filteredArray) // Output: [1, 3, 5]

In this example, the filterNotNull function is called on the array array, which returns a new array containing only the non-null elements. The resulting array is stored in the filteredArray variable.

By using filterNotNull, you can easily remove all null elements from an array in Kotlin.

The recommended way to remove elements from an array while iterating in Kotlin is to use a MutableIterator. Here's an example:

val numbers = mutableListOf(1, 2, 3, 4, 5) val iterator = numbers.iterator()

while (iterator.hasNext()) { val number = iterator.next() if (number % 2 == 0) { iterator.remove() // Remove the current element from the list } }

println(numbers) // Output: [1, 3, 5]

In this example, we have an array numbers containing integers. We create a MutableIterator using the iterator() function on numbers. Then, we iterate through the array using a while loop and the hasNext() and next() functions on the iterator. If a condition is met (in this case, if the number is even), we call the remove() function on the iterator to remove the current element from the array. Finally, we print the modified array.

What is the approach to remove empty strings from an array in Kotlin?

In Kotlin, one approach to removing empty strings from an array is to use the filter function along with the isNotEmpty or isNotBlank extension functions. Here's an example:

val array = arrayOf("Hello", "", "World", "", "!") val filteredArray = array.filter { it.isNotEmpty() }

println(filteredArray.joinToString(", "))

Output:

Hello, World, !

In the above example, we use the filter function on the array and pass a lambda function { it.isNotEmpty() } as a predicate. This lambda function checks if the current element is not empty using the isNotEmpty function. If it returns true, the element is included in the filtered array.

Alternatively, you can use the isNotBlank function instead of isNotEmpty to remove strings that contain only whitespace characters.

val array = arrayOf("Hello", " ", "World", " ", "!") val filteredArray = array.filter { it.isNotBlank() }

println(filteredArray.joinToString(", "))

Output:

Hello, World, !

The filter function creates a new list containing elements for which the predicate function returns true. Finally, joinToString is used to convert the filtered array into a string for printing.