I don t see why removeFirstNameDuplicates(Map<String, String> map) works. Pls, i realy need an explanation! Tk you!
public static void removeFirstNameDuplicates(Map<String, String> map) {
map = {"ab"-"ab", "ac"-"ac", "ad"-"ab", "ae"-"ab"...} // let say
ArrayList<String> list = new ArrayList<>(); // empty array
ArrayList<String> list1 = new ArrayList<>(); // empty array
for(String s: map.values()){
if(!list.contains(s)){ // if list is empty then first "ab"(value) is not yet in the list, so list.add(s) will be the next step(for passing the task we must delete from map all repeting value(all "ab")
list.add(s); //"ab", "ac"
}else
list1.add(s);//"ab"(from "ad"-"ab") and "ab"(from "ae"-"ab")
}
for(String s : list1){
removeItemFromMapByValue(map, s);
}
}
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());
}
}
}
I realy don t understand how this solution work
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
26 November 2021, 12:42useful
You have a bunch of values you compare a, b, c, a, a, d, b and you want to find the doubles. You iterate over that collection and put all these values in a basket (list - !contains(value)). If a value already is in that basket (if it is a double occurence), then you put it in a second basket (list1). In the end all values in list1 are double (or more) occurences. Iterate over that collection and call the given remove method for each entry - done.
+2
Angel Stefan
26 November 2021, 15:16
OK, we have list {"ab", "ac"} and in list1{"ab", "ab"}. Next step iterate over list1 with for(String s : list1) and remove from map each entry with the same value like String s.
Tk you!
0
Thomas
26 November 2021, 15:28
A set instead of list1 would be better though. Then you'd save redundant calls to the remove method in case there are more than two duplicates.
0
Guadalupe Gagnon
26 November 2021, 16:13
With code like this I would recommend that you draw it on a piece of paper (or any other form to visualize it). Take a starting map with any key-values that you want and then write out each step of the code. For example:
Take a Map<String, String> with these test values:
{"k1"-"v1", "k2"-"v2", "k3"-"v3", "k4"-"v2", "k5"-"v1", "k6"-"v4"}
Then describe how the algorithm works step by step:
Step one: iterate all the values at line " for(String s: map.values())"
Step two: value s (s will be "v1"), check if it is NOT contained in list (line "if(!list.contains(s))")
Step three (step 2 is true): add s to list (line list.add(s);)
Step four: no more lines in loop, start new cycle, s will now equal "v2"
Step five: value s , check if it is NOT contained in list
Step six (step 2 is true): add s to list (line list.add(s);)
etc etc.
Eventually you will see how "list1" ends up containing only the values that are in the map more than one time. This is a very good practice to do with code you don't understand. It is hard to do this without any visual help, which writing out the steps creates.
+1