Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

displaying the most highly repeated entry in an ArrayList

 
ankit chaubal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an arraylist named proteins. i need to find out and display which protein in the arraylist is repeated the most number of times. can anyone give me the code for it? some one suggested me to use maps but i have never worked with them. please help
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome to the JavaRanch.

You could loop through the List and count the number of times a protein appears. Could you show your code so that we can give some more precise pointers.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit, and welcome to the Ranch! You were already advised on java-forums.org how to use a Map<String, Integer> for this, and Jos even gave you sample code.

We don't have too many rules around here, but we do ask that you BeForthrightWhenCrossPostingToOtherSites.
 
Yunnan Zhou
Ranch Hand
Posts: 31
C++ Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use map like this:
 
ankit chaubal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thank you!! but i have a doubt. this will display all the entries along with the number of times each entry is repeated. i want only the one entry which is repeated the most number of times. how to do that???
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The one that is repeated the most will be the entry with the highest count.

Hunter
 
ankit chaubal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i got that! but i have been asked to print only the most highly repeated entry and not the others, whereas here the print statements prints all the entries in the map along with the number of times they are repeated. i have to print just one entry ie the most repeating entry only. how to do that?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ignore the map for a second. How would you find the maximum number in an array?

Hunter
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankit chaubal wrote:yes i got that! but i have been asked to print only the most highly repeated entry and not the others, whereas here the print statements prints all the entries in the map along with the number of times they are repeated. i have to print just one entry ie the most repeating entry only. how to do that?

You modify the code ! It's not that hard so why don't you have a go rather than just waiting for someone to do it for you. You'll learn much more that way.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We really don't like to just hand out code. We much prefer YOU to write it, ask specific questions, and we will help you fix/adjust/tweak it as needed.

Hunter has given you the best advice so far - but I'd go a little further. Forget JAVA all together. Describe, in english, how you personally would find which element in a list occurs the most number of times.

For example, if I needed to sort a list of items, I might do it like this:


read the first element of the list, and remember it.
Read every other element, and see if it any greater than the one i remember, and if so, remember it instead.
when i get to the end of the list, I have the greatest element left in the list.
Write that one down in a NEW list.
Cross it out of the old list
Start over on the old list.
when all the elements are crossed off the old list, the new list is complete and sorted.

So, do the same thing but explain how you would find the element that appears the most number of times in a list. You can use a paper and pencil to keep track of stuff, if you want, but explain what you write down, and how you change it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic