Menu
Coddy logo textTech

Creating Maps

Part of the Logic & Flow section of Coddy's Kotlin journey. Lesson 1 of 63.

A map associates each unique key with a value. mapOf exposes a read-only map; repeated keys keep the last supplied value. Kotlin's to syntax creates the key-value pairs passed to the factory.

Inside main:

val prices = mapOf("tea" to 4, "cake" to 7)
println(prices.size)

There are two distinct keys, so the map size is two.

Map keys are unique; several keys may share the same value.

challenge icon

Challenge

Medium

Complete entryCount with parameters first: String, second: String. Build a map mapping each supplied name to its length. Return the number of distinct keys. Names are nonempty.

Unless stricter bounds are stated above, collections contain at most 100 elements at each level, and integer arguments and integer collection values are between -1000 and 1000. Text supplied for parsing can include invalid or out-of-range representations as described.

Return a value of type Int. Keep the supplied input/output code. It reads scalar arguments one per line; a list starts with its count followed by its elements, and a map starts with its entry count followed by each key and value. Nested lists repeat the count-and-elements format for each row. The supplied main prints the return value followed by one newline. Lists use Kotlin's standard bracketed format; print no additional labels.

Try it yourself

fun entryCount(first: String, second: String): Int {
    // Write your solution here.
    return 0
}
fun main() {
    val first = readLine().orEmpty()
    val second = readLine().orEmpty()
    println(entryCount(first, second))
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow

Practice on your own: Kotlin playground