• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why sould I use map here?  RSS feed

 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hej all! Maybe i don't understand the concept very well, but I do not understand why should I use map here?
Can someone make this clear to me, any hints?


Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the
most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
If the list is empty, return 0.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, ask yourself -- What is a map? It's a set of key-value pairs, where the key is some data item and the value is information related to that data item. You need to keep track of integers along with the number of times each integer appears in the list.

Does that help? Can you see what the keys and values would be?
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so here is what I did. I know it's probaly lame, but I hope I got the idea of the exercise(no?). Can you please suggest what improvements should I make, because I feel this is not the best way this to be done.
Thank you, Werner.

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's actually not bad at all. You don't need the "mapTwo" map as far as I can see, but maybe it was left over from something you tried earlier. But apart from that it's a pretty good first attempt.

You could simplify your counting phase a bit by using a Map<Integer, AtomicInteger>, but that's for more advanced students. The code might look like this then:

 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to look up putIfAbsent.
Java 8...phooey.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Werner Holt wrote:Ok, so here is what I did. I know it's probaly lame, but I hope I got the idea of the exercise(no?).

Yes. And like Paul, I think you've pretty much nailed it.

Can you please suggest what improvements should I make, because I feel this is not the best way this to be done.

OK. A few:

1. Don't use names like 'list' (or especially 'mapTwo' which, as Paul said, is redundant anyway) because they're not very descriptive.
How about 'numbers' instead?

2. You could calculate 'max' without the second loop. Just move its code into your first one. However, the way you've done it is possibly easier to break up if you decide you need to, so it's not necessarily "better".

3. (This is more of a tip) You can save yourself a bit of typing by using Arrays.asList(), viz:
List<Integer> list = Arrays.asList(1, 4, 7, 9, 1, 1, 9, 1);

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!