Home > Net >  How to remove certain values in list but that value occurs more than one?
How to remove certain values in list but that value occurs more than one?

Time:11-15

Let's say I have

val asd = mutableListOf("lulu","bubu","gugu","bubu")

If I use asd.remove("bubu"), it only removes the first bubu.

How to remove all bubu in asd without a loop?

CodePudding user response:

Even a single remove() call is using a loop (under the hood). To remove all occurrences using a single loop under the hood, you can use removeAll { it == "bubu" }.

CodePudding user response:

You can use removeAll function that takes Collection as input which is array-List of string in this case. It will remove all occurences of all elements present in the parameter.

asd.removeAll(mutableListOf("bubu"))

Use this code and it should work now.

  • Related