We don't need repeats

  • 10
  • Locked
It's difficult to imagine a situation where you might need the program we're going to ask you to write. However, small modifications can make it practical. You can also imagine it as part of something larger, such as a search engine. Let's create a dictionary, fill it with (last name, first name) entries, and then remove people with the same first name.
You can't complete this task, because you're not signed in.
Comments (31)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Alexandre Desbois
Level 8 , Chamarandes-Choignes, France
27 December 2021, 07:19
I found it easier than the other difficult ones. I created two HashMap in removeFirstNameDuplicates, one copy of the map to iterate through it without having problems when we would remove elements from the original map and another to add the dupplicates in it. Then it's pretty straightforward.
carlos oliveira
Level 10 , Seattle, United States
20 November 2021, 03:38
-SUPER HINTS: - Only pass the "duplicated values" to removeItemFromMapByValue() method otherwise it will remove everithing. - when removing ((keep the method 1º parameter, and replace the 2º paramenter with the "key or value" that it's duplicated)) - remember: passing the node/set view/keyset won't work for the first parameter
Richard Caputo
Level 16 , United States of America
17 September 2021, 19:46
Biggest mistake I made was forgetting that ALL KEYS of a hashmap are unique.
ImDevin
Level 15 , Old Town, United States
30 April 2021, 02:59
As someone mentioned, do look up Collections.frequency. It will make your life much more pleasant. Happy coding! :)
ziv fisher
Level 9 , Petah Tikva, israel
12 October 2020, 20:36
This was fun and challenging!
Karas Java Developer
27 September 2020, 23:26
Couple of hints: The removeByValue Method will remove from the map any name passed in it as a value, NOTE: you are passing a map and a string. Second, remember you can not remove from the same map you are iterating. In few words, find out how many times a first name repeats, then remove those repeated more than 1. It is that simple
Michael
Level 10 , Dresden, Germany
13 June 2021, 19:41
Thank you, this is a perfect summary of the recipe to solve (if not to love) this very very challenging task! 👍👍👍
Petros
Level 23 , Columbia, United States
25 July 2020, 01:02
First answer I ever copy pasted. I gotta study up on HashMaps.
Joe M
Level 11 , Owings Mills, United States
3 January 2021, 18:27
Alex Lee youtube video on HashMaps was helpful for me. https://www.youtube.com/watch?v=70qy6_gw1Hc
Lucas Hoage
Level 14 , Savannah, United States
2 June 2020, 22:47
This is extremely difficult. Here are some hints. Once you finish the task, you'll understand the flow better: This does not require putting data into an ArrayList. This does not require inversion of a HashMap. This task can be completed using the following: A copy of map An Iterator A while loop A String that is initialized and assigned a foreach containing An entry A string variable that holds the "value" of entry A conditional check to check values between a copy and an entry that pipes the results into the last method Check results using system out in the second method and a call in the main method for the chain. How you decide to assign, initialize and order that information is up to you to figure out. Good luck.
Aakankasha Sharma
Level 18 , Chandigarh, India
14 June 2020, 09:19
Got it... but why is it not accepting solutions when I use an arraylist or a set for some purpose.. The tests went wild in that case. Also, why is creating a copy of the map better than storing values in an ArrayList?
Lucas Hoage
Level 14 , Savannah, United States
16 June 2020, 00:48
Been a while since I did this. I'm sure there's a way to do it using only the original map input, I simply created a copy to make it less troublesome for me at the time. Today I might have done it differently. You wouldn't want to use an array or a set because the method you're calling "removeItemFromMapByValue" is asking for Value from map of <String, String> type to be passed into it. If you tried passing a set or an array to that method, you'd get an error. An array and a set are not map types. Further, I wouldn't convert the map into another data structure type unless it was needed to pass information into a method requesting that through parameters. Why build something that isn't required? Why create one or several differing types of data structures just to manipulate the data, to be reconverted back into the original to be passed into the method? Do as little as possible as is needed. Be lazy. Also, although I haven't been able to confirm this, some tasks do not allow you to use certain types of code structures to complete the task. They want things done a very specific way. Other times I've been able to trick the validation doing things it specifically did not want. Touchy beast.
Justin Smith
Level 26 , Greenfield, USA, United States
14 July 2021, 01:51
Yeah, my solution passed but I feel like it wasn't what they wanted. My removeFirstNameDuplicates is 27 lines and creates 2 additional HashSets (to store a discrete list of first names, and to store a list of names which had duplicates).
Artur Verdyan
Level 24 , Hoboken, United States
17 March 2020, 18:44
The task is complicated but while solving it I understood something I would miss solving easy tasks.
Jason Huh
Level 16 , Bellevue, United States
12 February 2020, 19:59
First step would be to build a frequency map by the first name. Second step would be to loop through the frequency map and call removeItemFromMapByValue if the frequency of a first name >= 2.