Problem overview
The method toDictionary receives an Array[String] of names and an Array[Int] of scores with the same length. It returns a scala.collection.mutable.Map[String, Int] where each name is a key and the score at the same index is its value. Names are all different, two names may share a score, and empty arrays give an empty map.
For Array("Ana", "Ben", "Cara") and Array(90, 75, 88), the map has Ana mapped to 90, Ben to 75 and Cara to 88. For Array("Zoe", "Adam") and Array(60, 60), there are two entries, both with the value 60: equal values are fine, only keys must be unique.
Approach
The two arrays are linked by position: names(i) belongs with scores(i). So the loop runs over the indices rather than over the elements, and each pass uses one index to reach both arrays. Each pass adds one entry to a mutable map.
This pairing pattern comes up whenever data arrives as parallel lists, such as a column of names next to a column of numbers. The map makes the link explicit: after the loop you ask for a name and get its score, without remembering the position.
Step by step
- Create the result.
scala.collection.mutable.Map[String, Int]()in avalnamedresult: empty, and changeable. - Loop over the indices.
for (i <- 0 until names.length)visits 0, 1, 2 and so on up to one less than the length. The range0 until 3holds 0, 1 and 2;0 until 0is empty, so empty input does nothing. - Store the pair.
result(names(i)) = scores(i)adds the keynames(i)with the valuescores(i). - Return
result.
The solution in Scala
class Solution {
def toDictionary(names: Array[String], scores: Array[Int]): scala.collection.mutable.Map[String, Int] = {
val result = scala.collection.mutable.Map[String, Int]()
for (i <- 0 until names.length) {
result(names(i)) = scores(i)
}
return result
}
}
- Two sets of parentheses. In
result(names(i)) = scores(i), the innernames(i)reads an array element and the outerresult(...)is the map's key. Writing to a key that isn't there adds it, and writing to one that is there replaces its value. untilversusto.0 until nstops beforen.0 to nincludesn, which would read one element past the end of the arrays and throw anArrayIndexOutOfBoundsException.- Mutable map type. The result is changed as the loop runs, so it has to be
scala.collection.mutable.Map, not the default immutableMap. - A library shortcut.
names.zip(scores)pairs the two arrays into(name, score)tuples, andscala.collection.mutable.Map.from(names.zip(scores))builds a mutable map from them. The loop above does the same job and shows each step.
Edge cases and common mistakes
- Empty arrays. The range
0 until 0has no indices, so the loop never runs and the method returnsHashMap(). - Repeated names. The lesson promises distinct names. If a name did repeat, the later score would replace the earlier one: with names
Array("Ana", "Ana")and scoresArray(1, 2)the map ends up asHashMap(Ana -> 2). - Looping over the values. With
for (name <- names)there is no index to find the matching score. Use the index range. - Different lengths. If
scoreswere shorter thannames, the loop would throw when it ran off the end ofscores. The lesson guarantees equal lengths.
Testing
Array("Ana", "Ben", "Cara"),Array(90, 75, 88)givesHashMap(Cara -> 88, Ana -> 90, Ben -> 75).Array("Zoe", "Adam"),Array(60, 60)givesHashMap(Adam -> 60, Zoe -> 60).Array("Max"),Array(100)givesHashMap(Max -> 100).- Two empty arrays give
HashMap().
