Any idea or suggestion ??
package com.codegym.task.task08.task0817;
import java.util.*;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> map = new HashMap();
map.put("A", "AAAA");
map.put("B", "SSSS");
map.put("C", "BBBB");
map.put("D", "AAAA");
map.put("E", "CCCC");
map.put("F", "AAAA");
map.put("G", "AAAA");
map.put("H", "CCCC");
map.put("I", "AAAA");
map.put("J", "SAQSS");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
int count = 0, no = 0;
HashMap<String, String> copy = new HashMap<String, String>(map);
Iterator<Map.Entry<String, String>> itr = copy.entrySet().iterator();
while (itr.hasNext()) {
String value1 = itr.next().getValue();
Iterator<Map.Entry<String, String>> itr2 = copy.entrySet().iterator();
while(itr2.hasNext()) {
String value2 = itr.next().getValue();
if (value1.equals(value2)) {
count++;
}
if (count > 1) {
removeItemFromMapByValue(map, value2);
no = 1;
}
}
if (no == 1) {
removeItemFromMapByValue(map, value1);
}
no = 0;
}
}
public static void removeItemFromMapByValue(Map<String, String> map, String value) {
HashMap<String, String> copy = new HashMap<String, String>(map);
for (Map.Entry<String, String> pair : copy.entrySet()) {
if (pair.getValue().equals(value))
map.remove(pair.getKey());
}
}
public static void main(String[] args) {
//HashMap<String, String> map = createMap();
//System.out.println(map);
//removeFirstNameDuplicates(map);
//System.out.println(map);
}
}