oday
02/01/2021, 8:08 PMval map = mutableMapOf<Char, Int>()
map['a'] += 1
Russell Stewart
02/01/2021, 8:16 PMa
in the map yet. If you're trying to add a value to it, you would want:
map['a'] = 1
Or just set it in the definition:
val map = mutableMapOf<Char, Int>('a' to 1)
oday
02/01/2021, 8:16 PMRussell Stewart
02/01/2021, 8:16 PMval map = mutableMapOf<Char, Int>('a' to 1)
map['a'] += 1
Haris Khan
02/01/2021, 8:18 PM+= is effective plusAssign
plusAssign's receiver canot be null but map["a"] is a nullable type.oday
02/01/2021, 8:18 PMfun canConstruct(ransomNote: String, magazine: String): Boolean {
val letters = mutableMapOf<Char, Int>()
magazine.forEach {
if (it != ' ') {
letters[it] += 1
}
}
Russell Stewart
02/01/2021, 8:19 PM= 1
instead of += 1
.oday
02/01/2021, 8:19 PMval map = mutableMapOf<Char, Int>('a' to 1)
map['a'] += 1
doesn’t work, same errorRussell Stewart
02/01/2021, 8:20 PM1
for each character, then do:
magazine.forEach {
if (it != ' ') {
letters[it] = 1
}
}
oday
02/01/2021, 8:21 PMRussell Stewart
02/01/2021, 8:21 PModay
02/01/2021, 8:22 PMmagazine.forEach {
letters[it] += 1
}
same error hereRussell Stewart
02/01/2021, 8:23 PMmagazine.forEach {
if (it != ' ') {
if (letters.containsKey(it) {
letters[it] += 1
} else {
letters[it] = 1
}
}
}
oday
02/01/2021, 8:23 PMRussell Stewart
02/01/2021, 8:24 PModay
02/01/2021, 8:41 PMmap[it] = (map[it] ?: 0) + 1
Russell Stewart
02/01/2021, 8:42 PModay
02/01/2021, 8:44 PMRussell Stewart
02/01/2021, 8:45 PMnanodeath
02/01/2021, 9:44 PMval foo = "hello world"
val counts: Map<Char, Int> = foo.groupingBy { it }.eachCount()