04/02/2023

Day 8 Dictionaries and Maps Hackerrank Solution in C++ | 30 Days

Write a Program to Day 8 Dictionaries and Maps Hackerrank Solution in C++. Hackerrank C++ Dictionaries and Maps Solution. 30 Days of Code Solutions in C++. Given names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. for each query, print the associated entry from your phone book on a new line in the form name=phonenumber.

Day 8 Dictionaries and Maps Hackerrank Solution in C++

If an entry is not found, print not found instead. note: your phone book should be a dictionary/map/hashmap data structure. Always try to implement your own function and logic this will help you to solve and build a logic, But you have to know how to use the predefined function.

Submit Your Solution Here: Click Here

Day 8 Dictionaries and Maps Hackerrank Solution in C++


#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

#include<map>

using namespace std;
int main() {
  /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  int n;
  string name;
  long num;
  cin >> n;
  cin.ignore();
  map < string, long > pbook;
  for (int i = 0; i < n; i++) {
    cin >> name;
    cin >> num;
    pbook[name] = num;
  }
  while (cin >> name) {
    if (pbook.find(name) != pbook.end())
      cout << name << "=" << pbook.find(name) -> second << endl;
    else
      cout << "Not found" << endl;
  }
  return 0;
}

Dictionaries and Maps Logic


Day 8 Dictionaries and Maps Hackerrank Solution. as we know that Dictionary is a collection of data(A variety of data). So for this problem, we can take the example of the phone book. The phonebook consists of various phone data line names, addresses and phone numbers, we are considering only phone names and numbers. We can solve this problem by considering a phone book. See the below explanation.

Explanation of Dictionaries and Maps Problem


Suppose we have a phone book containing a name and phone number.

1. Ghanendra 9999999999
2. Prashant 8888888888
3. Pramod 7777777777
4. John 9898989898
5. ............................
6. ............................
n.......... So On.

Now if a person wants a number of particular people then he will enter a person's name or multiple person names. Now the first person came and enter the name of the desired people.

Person 1:

Enter name = Ghanendra
Output => Ghanendra = 9999999999

Person 2:

Enter name = Prashant
Output => Prashant = 8888888888

Person 3:

Enter name = John
Output => John = 9898989898

Person 4:

Now person 4 came and enter a name in Smith, as we know that Smith is not stored in our dictionary then our program prints' Not found '. See the below example.

Enter name = Smith
Output = Not found

Person 1: 

Now again if person 1 enters the name Pramod then it will print the Pramod details if exist.

Enter name = Pramod
Output => Pramod = 7777777777

I hope you got the problem statement and logic of the problem, Now we can solve the problem by implementing our own function and logic or we can use the existing function. Here I am using an existing function that will help me to save time.

Dictionaries and Maps Hackerrank Solution Output


Dictionaries and Maps Hackerrank Solution Output

Similar to Dictionaries and Maps


  • Hackerrank Solution For Day 9: Recursion
  • Hackerrank Solution For Day 10: Binary Numbers
  • Hackerrank Solution For Day 11: 2D Arrays
  • Hackerrank Solution For Day 12: Inheritance
  • Hackerrank Solution For Day 13: Abstract Classes
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: