Maps in C#

Hello Techies,

C# doesn't have built-in maps. Today, we will learn how to use a dictionary to implement maps in C#. 

Dictionary

The Dictionary is a generic type or an array that stores the value in key-value pair. Dictionary In C# (c-sharpcorner.com)

Maps

C# doesn't have any built-in Maps. The Maps is the concept that provides the functionality to map the value with the key inside the Dictionary. So a map is used inside the Dictionary for mapping the key-value pair. 

So, let's just quickly go through the statement that we are going to solve today,

Que

So, here we have a task to create a simple phonebook with the respective name as a key, and the value of the name is a string that stores the number, which will be a phone number for the respective name. Then the user will give some words we must match with the key. If the name is found, we must print ("name = Phone number"). Else, "Not Found." Isn't it easy to hear and easy to implement?

Ok, let's start building the solution by the first step to define the Dictionary. First, let's go through the following code.

Dictionary < string, string > phonebook = new Dictionary < string, string > ();

So, you can see the Dictionary, which is similar to an Array of Objects if you are aware of the concept, something like PhoeBook =[{name: string ,number : string}];

So, this is an array, and if we want to store the multiple values, we require two things; the first is a variable to get the total number of an element the user wishes to enter, and the second thing is an array to get the details from the user and store that to the Dictionary.

Let's check out the following snippet to get the number of elements from the user.

int n = Int32.Parse(Console.ReadLine());

for (int i = 0; i < n; i++) 
{
     string[] line = Console.ReadLine().Split(' '); //getting the key value by space separated..
     phonebook[line[0]] = line[1];    // storing the vslue to Dictionary as key = value.
}

Let's move further here. We get the values inside the Dictionary.

So, let's move further to check out the values the user enters to check whether the matter is inside the Dictionary. Now, here we are, corresponding with the name directly. So,  let's get the words inside the array we want to find and then iterate that array with the Dictionary Key to see the value and return based on the result we get.

So, this will get too complex and long a solution for a short statement. So, in all my blogs and articles, I have said to think a little more to make the code optimized and reusable for any other project. So, we can create a function and declare one variable with a loop, and at the runtime of the loop, we initialize it with the value. Confused ...?

Let's check out the following snippet for more information and clear the doubt.

static void Check_name(Dictionary < string, string > phonebook) {
    string name;
    while ((name = Console.ReadLine()) != null && name.Length > 0) {
        if (phonebook.ContainsKey(name)) {
            Console.WriteLine(name + "=" + phonebook[name]);
        } else {
            Console.WriteLine("Not found");
        }
    }
}

Here, I have just got the value from the user and applied the conditions on the name itself. So, we can iterate the loop with the condition based on the name and get the value based on the condition.

Check out the full solution here -  Compile code

Here is the full source code for the solution.

Note
This is the solution for the HackerRank 30DaysofCode Coding challenge Day 8 Dictionary and Maps.

using System;
using System.Collections.Generic;
class Solution 
{
    static void Main(String[] args) 
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your
        class should be named Solution */
        
        int n = Int32.Parse(Console.ReadLine());
        Dictionary < string,
        string > phonebook = new Dictionary < string,
        string > ();

        for (int i = 0; i < n; i++) {
            string[] line = Console.ReadLine().Split(' ');
            phonebook[line[0]] = line[1];
        }
        Check_name(phonebook);
    }
    static void Check_name(Dictionary<string,string> phonebook)
    {
        string name;
        while ((name = Console.ReadLine()) != null && name.Length>0) {
            if (phonebook.ContainsKey(name)) {
                Console.WriteLine(name + "=" + phonebook[name]);
            }
            else {
                Console.WriteLine("Not found");
            }
        }
    }
}