To remove a specific element from a list in Kotlin, you can use the `remove` method.
In this example,
list
with some elements.remove
method of the list to remove the element if it is present.fun main() {
val list = mutableListOf(1, 2, 3, 4, 5)
val elementToRemove = 3
list.remove(elementToRemove)
println("Modified list after removing $elementToRemove: $list")
}
Modified list after removing 3: [1, 2, 4, 5]
In this example,
list
with some elements.remove
method of the list to attempt to remove the element.fun main() {
val list = mutableListOf(1, 2, 3, 4, 5)
val elementToRemove = 6
list.remove(elementToRemove)
println("Original list: $list")
}
Original list: [1, 2, 3, 4, 5]
In this tutorial, we learned How to Remove Specific Element from a List in Kotlin language with well detailed examples.