Problem overview
The method ToDictionary receives a string[] named names and an int[] named scores of the same length. It pairs each name with the score at the same index and returns a Dictionary<string, int> where names[i] is a key and scores[i] is its value.
For names = ["Ana", "Ben", "Cara"] and scores = [90, 75, 88], the result is Ana 90, Ben 75, Cara 88. The names are all different, but two names may share a score: ["Zoe", "Adam"] with [60, 60] gives two entries. Two empty arrays give an empty dictionary.
Approach: one index reaches both arrays
A name and its score sit at the same position, so a single counter i is enough to reach both: names[i] and scores[i]. Run i from 0 to the last position, and in each round store one pair in the dictionary.
That is the reason for a for loop here and not a foreach. A foreach over names gives the names but not their positions, and the score is found by position.
Storing a pair is a single assignment, result[key] = value. If the key isn't in the dictionary yet, the assignment adds it, so there is nothing to check first.
On the first example, i = 0 stores Ana with 90, i = 1 stores Ben with 75 and i = 2 stores Cara with 88. After three rounds the dictionary has three entries and the loop ends.
The dictionary doesn't care which array is longer or shorter, only that the loop reads positions that exist in both. That is why the lesson guarantees equal lengths.
Step by step
- Create an empty
Dictionary<string, int>namedresult. It gets one entry per name. - Write a
forloop with anintithat starts at 0 and goes up by one whilei < names.Length. For an array,Lengthis the number of items. - Store the pair with
result[names[i]] = scores[i]. The name is the key, the score is the value. - Return
resultafter the loop.
The solution in C#
class Solution {
public Dictionary<string, int> ToDictionary(string[] names, int[] scores) {
Dictionary<string, int> result = new Dictionary<string, int>();
for (int i = 0; i < names.Length; i++) {
result[names[i]] = scores[i];
}
return result;
}
}
Details worth knowing:
- The method is named
ToDictionary, and C# also has a LINQ method with that name. The lesson's method is the one inclass Solutionand works on its own, with a plain loop. - The loop uses
names.Lengthas the bound. This is correct because the two arrays have the same length. Ifscoreswere shorter,scores[i]would throw anIndexOutOfRangeException. - With empty arrays the loop body never runs and the method returns the empty dictionary it created.
Edge cases and common mistakes
- Repeated names. The lesson promises that names are different. If a name did repeat, the assignment would silently overwrite the earlier score, so the later score would win: for
["Ana", "Ana"]with[1, 2]the result is the single entryAna: 2. TheAddmethod of a dictionary would throw anArgumentExceptionfor a repeated key instead. - Mixing up key and value.
result[scores[i]] = names[i]wouldn't compile, because the keys are strings. - Same score twice. Equal scores are fine: only keys must be unique, and values can repeat.
- Off-by-one bounds.
i <= names.Lengthwould read one position past the end and throw.
Testing
| names | scores | Result |
|---|---|---|
["Ana", "Ben", "Cara"] | [90, 75, 88] | Ana: 90, Ben: 75, Cara: 88 |
["Zoe", "Adam"] | [60, 60] | Zoe: 60, Adam: 60 |
| empty | empty | empty dictionary |
