David Peter
David Peter

Reputation: 45

How can I remove the char 'a' from a list?

I have this code :

fun main(args:Array<String>){

    var a = "eat, banana, one"
    var a1 = a.split(",").toMutableList()
    a1.sortBy { it.toCharArray().count { it == 'a' } }
    var a2 = a1.associateWith { word -> word.count { char -> char == 'a' } }

    a2.keys.filterNot { c -> "a".contains(c)}
   }

Actually, I want to remove the "a" in the word that I have using this line : a2.keys.filterNot { c -> "a".contains(c)} but it does not work.

How could I do to remove all the a in a2 ?

Thank you very much !

Upvotes: 0

Views: 66

Answers (2)

Simulant
Simulant

Reputation: 20112

you can map the keys to a new map and replace the a with an empty String in the keys. You then need to use the new created map as result:

fun main(args:Array<String>){
    val a = "eat, banana, one"
    val a1 = a.split(",").toMutableList()
    a1.sortBy { it.toCharArray().count { it == 'a' } }
    val a2 = a1.associateWith { word -> word.count { char -> char == 'a' } }

    val result = a2.mapKeys { it.key.replace("a", "")}
    println(result) // prints { one=0, et=1,  bnn=3}
}

Upvotes: 0

s1m0nw1
s1m0nw1

Reputation: 81929

In order to remove all a characters from your keys, you can replace them with an empty string:

a2.mapKeys { it.key.replace("a", "")}

Upvotes: 1

Related Questions