package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> names = new HashMap<>();
names.put("John", "Doe");
names.put("John1", "Doe1");
names.put("John2", "Doe2");
names.put("John3", "Doe3");
names.put("John4", "Doe4");
names.put("John5", "Doe");
names.put("John6", "Doe1");
names.put("John7", "Doe2");
names.put("John8", "Doe3");
names.put("John9", "Doe4");
return names;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
HashMap<String, String> cloneMap = new HashMap<String, String>(map);
map.clear();
for (Map.Entry<String, String> kv : cloneMap.entrySet()) {
if (!map.containsValue(kv.getValue())) {
map.put(kv.getKey(), kv.getValue());
} else {
removeItemFromMapByValue(map, kv.getValue());
}
}
}
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(map.values()))
map.remove(pair.getKey());
}
}
public static void main(String[] args) {
//removeFirstNameDuplicates(createMap());
}
}
Desperate Attempt #2
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
17 March 2019, 15:25
It is probably failing because the verification system may be checking 'map' as the code runs. If this is the case then when you clear it at line 33 verification is probably automatically failing. Try replacing line 33 with creating a HashSet<String>. Then in the for each loop, use the same logic with the new HashSet instead of 'map' (checking if the set contains the value and adding it if it does not).
+2
// Java Poser
18 March 2019, 00:44
Thank you very much Guadalupe, I finally figured it out. took me a few days lmao
0