We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
In Java I was also getting a Timeout error, my guess is that because I was stroring the results (output) in a string , rather than printing them out one at a time.
//Complete this code or write your own from scratch
import java.util.;
import java.io.;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Map phoneBook= new HashMap();
for(int i = 0; i < n; i++){
String name = in.next();
int phone = in.nextInt();
// Write code here
phoneBook.put(name,phone);
}
String results="";
int searchNumber=0;
while(in.hasNext()){
String s = in.next();
searchNumber++;
// Write code here
Integer num=phoneBook.get(s);
if(num != null){
System.out.print(s+"="+ num +"\n") ;
}
else
System.out.print("Not found\n") ;
// results+="Not found\n";
}
in.close();
// System.out.print(results);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 8: Dictionaries and Maps
You are viewing a single comment's thread. Return to all comments →
In Java I was also getting a Timeout error, my guess is that because I was stroring the results (output) in a string , rather than printing them out one at a time.
//Complete this code or write your own from scratch import java.util.; import java.io.;
class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int n = in.nextInt(); Map phoneBook= new HashMap();
}