TELEPHONE BOOK USING HASHMAP

 

Sample Input

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry

Sample Output

sam=99912222
Not found
harry=12299933

Explanation

We add the following  (Key,Value) pairs to our map so it looks like this:

We then process each query and print key=value if the queried  is found in the map; otherwise, we print Not found.

Query 0: sam
Sam is one of the keys in our dictionary, so we print sam=99912222.

Query 1: edward
Edward is not one of the keys in our dictionary, so we print Not found.

Query 2: harry
Harry is one of the keys in our dictionary, so we print harry=12299933.


SOLUTION:

package HackerRank_problems.practice_problems;
import java.util.*;
import java.io.*;

public class TelephoneBook{
    public static void main(String []argh){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        HashMap<String,Integerhm=new HashMap();
        for(int i = 0i < ni++){
            String name = in.next();
            int phone = in.nextInt();
            hm.put(name,phone);
            
        }
        while(in.hasNext()){
            String s = in.next();
            if(hm.get(s)==null)
            {
                System.out.println("Not found");
            }
            else{
                System.out.println(s+"="+hm.get(s));
            }
        }
        in.close();
    }
}

Comments

Popular posts from this blog

PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION

PROGRAM TO FIND THE COUNT OF PARTICULAR NUMBER IN AN ARRAY