Java Collection | Set 3 (HashMap) Part-1
Basic Accuracy: 60.0% Submissions: 1274 Points: 1

HashMap is a part of collection in Java since 1.2. It provides the basic implementation of Map interface of Java. It stores the data in (Key, Value) pairs. To access a value you must know its key, otherwise, you can’t access it. HashMap is known as HashMap because it uses a technique Hashing. Try this problem using HashMap.

Given n key - value pairs and a seperate key in next line. If this key is found in the previous n keys then print its value otherwise print "-1".

Input: First line of input contains a single integer T which denotes the number of test cases. T test cases follows, first line of each test case contains a integer n. Second line of each test case consists of n key(String)-value(Integer) pairs space separated. Third and last line of each test case consists of a single key(String).

Output: If the key is found in the HashMap print its value otherwise print "-1".

Constraints:

1<=T<=100
1<=n,|Length of key|,value<=1000

Example:

Input:
2
3
sak 5 varun 7 vijay 3
sak
4
csdb 4 dsh 5 askj 8 adfs 9
dptu

Output:
5
-1

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.