30 days of code: Day 8

https://www.hackerrank.com/domains/tutorials/30-days-of-code

Using a Dictionary
Sample Input

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
Sample Output

sam=99912222
Not found
harry=12299933
// Input number of data to retrieve
int nameCount =Convert.ToInt32(Console.ReadLine());

// Create Dictionary
Dictionary<string, string> phoneBook = new Dictionary<string, string>();

// User adds name and phonenumber to Dictionary x times
for (int i = 0; i < nameCount; i++)
{
    string inputPhoneBook = Console.ReadLine();
    string[] nameAndPhoneNumber = inputPhoneBook.Split(" ");
    string name = nameAndPhoneNumber[0];
    string phoneNumber = nameAndPhoneNumber[1];

    phoneBook.Add(name, phoneNumber);
}

// Name search and output infinite times
while (true)
{
    string inputName = Console.ReadLine();
    if (!string.IsNullOrEmpty(inputName))
    {
        if (phoneBook.ContainsKey(inputName))
        {
            Console.WriteLine(inputName + "=" + phoneBook[inputName]);
        }
        else
        {
            Console.WriteLine("Not found");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *