Start learning free

LESSON 128 OF 677 · DICTIONARIES

Dictionary Length in Kotlin

Lesson

In Kotlin, you can get the number of key-value pairs in a Map using its size property:

val scores = mutableMapOf<String, Int>()
scores["Alice"] = 95
scores["Bob"] = 87
scores["Charlie"] = 72

val count = scores.size
println(count)  // 3

size is the number of entries. You can also check if it's empty with isEmpty():

println(scores.isEmpty())  // false
val empty = mutableMapOf<String, Int>()
println(empty.size)        // 0

Assignment
Follow the Coding Tutorial and get the number of entries in scores!


Hint
Use scores.size and assign it to count.

STEP 1 OF 1
    Ctrl + Enter to check