Java Collection | Set 3 (HashMap) Part-1
Submissions: 810   Accuracy:

57.77%

  Difficulty: Basic   Marks: 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

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: S_A
Author: saksham0751


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.