Start learning free

LESSON 229 OF 289 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Two Arrays to a Dictionary in C#: Pairing Names with Scores

Practice Read the full explanation

Given an array of strings names and an array of integers scores of the same length, pair each name with the score at the same index. Return a Dictionary where names[i] is a key and scores[i] is its value.

The names are all different, while two names can have the same score. If names and scores are empty, return an empty Dictionary. The order of the entries doesn't matter.

Example 1:

Input: names = ["Ana", "Ben", "Cara"], scores = [90, 75, 88]
Output: {"Ana": 90, "Ben": 75, "Cara": 88}
Explanation: "Ana" is at index 0 in names, and the score at index 0 is 90. The same goes for "Ben" with 75 and "Cara" with 88.

Example 2:

Input: names = ["Zoe", "Adam"], scores = [60, 60]
Output: {"Zoe": 60, "Adam": 60}
Explanation: Two names with the same score are two different entries.

Example 3:

Input: names = ["Max"], scores = [100]
Output: {"Max": 100}
Explanation: One name gives a single entry.
STEP 1 OF 4
    Ctrl + Enter to check

    C# · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Two Lists To A Dictionary in C#, explained

    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

    1. Create an empty Dictionary<string, int> named result. It gets one entry per name.
    2. Write a for loop with an int i that starts at 0 and goes up by one while i < names.Length. For an array, Length is the number of items.
    3. Store the pair with result[names[i]] = scores[i]. The name is the key, the score is the value.
    4. Return result after 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 in class Solution and works on its own, with a plain loop.
    • The loop uses names.Length as the bound. This is correct because the two arrays have the same length. If scores were shorter, scores[i] would throw an IndexOutOfRangeException.
    • 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 entry Ana: 2. The Add method of a dictionary would throw an ArgumentException for 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.Length would read one position past the end and throw.

    Testing

    namesscoresResult
    ["Ana", "Ben", "Cara"][90, 75, 88]Ana: 90, Ben: 75, Cara: 88
    ["Zoe", "Adam"][60, 60]Zoe: 60, Adam: 60
    emptyemptyempty dictionary
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free