Dictionary And Maps In C#

Hello Techies,

Today, we are going to learn the Dictionary and the Maps by solving a simple statement which I have described below.  First, I'll explain what  Dictionary and Maps are in a simple way.

Dictionary

The Dictionary is a generic type or we can say an array that stores the value in key-value pair.

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 we can say a map is used inside the dictionary for mapping the key-value pair.

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

Que

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

Ok, let's just start building the solution by the first step to define the dictionary. Let's just 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 Object 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; first is variable to get the total number of element user want to enter, and the second thing is an array to get the elements from the user and store that to the Dictionary.

Let's just 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 just move further to check out the values the user enters to check whether the value is there inside the Dictionary or not. Now, here we are checking with the name directly. So,  let's just get the names inside the array which we want to find and then iterate that array with the Dictionary Key to find the value and return based on the result we get.

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

Let's just 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 we 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");
            }
        }
    }
}