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

67 Views Asked by At

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?

2

There are 2 best solutions below

0
abhishekrajak On BEST ANSWER

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.

0
Tenfour04 On

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" }.