Professional Documents
Culture Documents
import java.util.*;
class Source {
int n = emails.length;
if (emails[i].equals(key))
return i;
return -1;
String[] emails =
{"chandler.bing@xyz.com","ross.geller@xyz.com","rachel.greene@xyz.com","joey.tribbiani@xyz.co
m","monica.geller@xyz.com","phoebe.buffay@xyz.com","sheldon.copper@xyz.com","marie.george
@xyz.com"};
String NewEmail="monica.geller@xyz.com";
System.out.println(findDuplicateEmail(emails,NewEmail));
}
2. Hash Table to Check Registered Email IDs
import java.util.*;
class Source {
emails.put("chandler.bing@xyz.com",1);
emails.put("ross.geller@xyz.com",2);
emails.put("rachel.greene@xyz.com",3);
emails.put("joey.tribbiani@xyz.com",4);
emails.put("monica.geller@xyz.com",5);
emails.put("phoebe.buffay@xyz.com",6);
emails.put("sheldon.copper@xyz.com",7);
emails.put("marie.george@xyz.com",8);
if(emails.containsKey(NewEmail))
//Complete here
System.out.println(emails.get("monica.geller@xyz.com"));
else {
System.out.println("False");
}
3. The Airline Boarding-Pass Problem
import java.util.*;
import java.util.Scanner;
class Source {
arr[i] = scanner.nextInt();
for(int i:arr)
if(boardingPass.get(i) == "CHECKED") {
System.out.println(i);
break;
} else {
boardingPass.put(i, "CHECKED");
}
4. Anagram of key value pairs of a HashMap
import java.util.*;
if (ch1.length != ch2.length)
return false;
if (ch1[i] != ch2[i])
return false;
return true;
int tmp;
tmp = ch[i];
ch[i] = ch[j];
}
}
String key;
String value;
key = sc.next();
value = sc.next();
map.put(key,value);
key = entry.getKey();
value = entry.getValue();
if (check(key, value))
anagramsMap.put(key, value);
if (anagramsMap.isEmpty())
System.out.println("false");
else
}
5. Find Symmetric Pairs
import java.util.Scanner;
import java.util.HashMap;
class Source {
int n = in.nextInt();
arr[i][0] = in.nextInt();
arr[i][1] = in.nextInt();
int no_of_pairs=0;
no_of_pairs++;
else
MatchPair.put(firstElement, secondElement);
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
if (!characterCount.containsKey(ch)) {
characterCount.put(ch, 1);
} else {
if (characterCount.get(ch) == 1) {
firstUniqueChar = ch;
break;
if (firstUniqueChar == null) {
System.out.println("-1");
} else {
System.out.println(firstUniqueChar);
}
7. Sets in Java
Description
You are given an integer ‘n’, which represents the number of operations to be performed on a
HashSet.
import java.util.*;
class Source {
int n = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
switch (a) {
case 1:
hashSet.add(b);
break;
case 2:
break;
case 3:
// Write your code here to remove the element b from the hashSet
hashSet.remove(b);
break;
import java.util.*;
class Source {
int n, count = 0;
n = in.nextInt();
array[i] = in.nextInt();
if (n == 0) {
System.out.println("false");
return;
hashSet.add(array[i]);
count++;
currentElement--;
}
/*Now to check for the elements greater than the array[0]. So, modify the "currentElement" as
array[0] + 1 */
currentElement = array[0] + 1;
count++;
currentElement++;
if (count == (hashSet.size())) {
System.out.println("true");
else
System.out.println("false");
import java.util.*;
class Source
{
int n = in.nextInt();
for(int i =0;i<n;i++){
array[i] = in.nextInt();
if (s.contains(temp)) {
System.out.println("true");
System.exit(0);
s.add(array[i]);
System.out.println("false");
} }
10. Find Itinerary From all Tickets
Description
You will be given the source and destination of all the tickets in the form of a map, and you have
to print the itinerary from all those tickets.
Note:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
int n = in.nextInt();
// Store the source and destination of the tickets to the map "tickets"
tickets.put(in.next(), in.next());
in.nextLine();
reverseTickets.put(entryTickets.getValue(), entryTickets.getKey());
if (!reverseTickets.containsKey(entryTickets.getKey())) {
start = entryTickets.getKey();
break;
if (start == null) {
System.out.println("Invalid");
return;
/* By now you know what is the start of itinerary, so print the itinerary */
to = tickets.get(from);
from = to;
}
Note:
• You should not compare any lock with any other lock; similarly, you should not compare
any key with any other key.
• The size of the array of locks and keys is the same.
import java.util.HashMap;
import java.util.Scanner;
class Source {
int n = in.nextInt();
keys[i] = in.next(".").charAt(0);
in.nextLine();
locks[i] = in.next(".").charAt(0);
hashMap.put(locks[i], i);
if (hashMap.get(keys[i]) != null) {
locks[i] = keys[i];
} else {
System.out.println("Not possible");
return;
}
// print the locks and keys in the order corresponding to the keys.
Example:
Map = {1=A,2=A,3=B}
Output: A B
import java.util.*;
String value;
for(int i : map.keySet()){
value = map.get(i);
if(h.get(value) == null){
h.put(value,i);
}
for(String s: h.keySet()){
System.out.print(s+" ");
int key;
String value;
key = sc.nextInt();
value = sc.next();
map.put(key,value);
printValues(map);