If I understood correctly, as soon as a person shares the same first name with a 2nd person, then we remove them from the map. And my code does that, however it fails validation: " The removeFirstNameDuplicates() method must remove from the map all people who have the same first name. "
Thanks in advance!
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> map = new HashMap<>();
map.put("Bogdan", "Vadim");
map.put("Tudor", "Vadim");
map.put("Dumitru", "Vasile");
map.put("Ioan", "Badea");
map.put("Petrica", "Badea");
map.put("Stanca", "Tudor");
map.put("Mazilu", "Toader");
map.put("Carmen", "Hara");
map.put("Body", "Vadim");
map.put("Vasile", "Vadim");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
ArrayList<String> firstNames = new ArrayList<>(map.values());
ArrayList<String> repeatedNames = new ArrayList<>();
for (int i = 0; i < firstNames.size(); i++) {
for (int j = 0; j < firstNames.size(); j++) {
if (firstNames.get(i).equals(firstNames.get(j)) && i != j){
repeatedNames.add(firstNames.get(i));
}
}
}
for (int i = 0; i < repeatedNames.size() ; i++) {
removeItemFromMapByValue(map, repeatedNames.get(i));
}
}
public static void removeItemFromMapByValue(Map<String, String> map, String value) {
boolean isIn = false;
for (Map.Entry<String, String> pair: map.entrySet()){
if (pair.getValue().equals(value)){
isIn = true;
}
}
if(isIn) {
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String s = iterator.next();
if (map.get(s).contains(value)) {
iterator.remove();
}
}
}
}
public static void main(String[] args) {
HashMap<String, String> map = createMap();
for (Map.Entry<String, String> pair: map.entrySet()){
System.out.println(pair.getKey() + " " + pair.getValue());
}
removeFirstNameDuplicates(map);
System.out.println("-----------------------------------------------------------------");
for (Map.Entry<String, String> pair: map.entrySet()){
System.out.println(pair.getKey() + " " + pair.getValue());
}
}
Ouput looks good, task is not validated
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Mike McKenna
29 November 2019, 22:56
Hi,
GG is correct . if u would change names so there are fewer repeats .
Hope i helped. let me know if i can be of more help
0
Guadalupe Gagnon
29 November 2019, 22:17
This code will only remove the very first 'first name' duplicate that it finds. For example, in the createMap method there are 2 names that are repeated more than once: "Vadim" and "Badea". When I run the code it will successfully remove "Vaidm", but "Badea" is still within the map and it should not be.
0
catalinene
30 November 2019, 11:42
Thank you for pointing out the problem. Fixed it, but is stil not geting validated.
I edited the post, with my new code. The code in main is for a quick test of the output.
0
Guadalupe Gagnon
30 November 2019, 19:08solution
it looks like you changed the default code. Put the code back in that is supposed to be in the removeItemFromMapByValue method.
+4
catalinene
30 November 2019, 19:11
Thanks a lot!
+1