Choose uniqueness or lookup explicitly
A set stores unique elements according to equality. A map associates keys with values, and inserting the same key replaces its previous value. Choose these structures based on the question your code asks.
fun main() {
val tags = setOf("Kotlin", "Compose", "Kotlin")
check(tags.size == 2)
val minutes = mutableMapOf("Kotlin" to 25)
minutes["Kotlin"] = 50
check(minutes["Kotlin"] == 50)
check(minutes["AI"] == null)
}
A map lookup returning null can mean either a missing key or a stored null if its value type is nullable; use containsKey when that distinction matters. Avoid mutating equality-relevant properties of hash keys after insertion.
Iteration order depends on the concrete collection contract; do not rely on arbitrary set order for ranked output. Sort explicitly when presentation order matters.
Exercise
Count topic frequencies from a list, then return the unique topics alphabetically. Test repeated entries and empty input.
Check: explain why a set alone cannot preserve the number of occurrences.