File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes displaying the most highly repeated entry in an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "displaying the most highly repeated entry in an ArrayList" Watch "displaying the most highly repeated entry in an ArrayList" New topic
Author

displaying the most highly repeated entry in an ArrayList

ankit chaubal
Greenhorn

Joined: May 16, 2011
Posts: 3
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

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Yunnan Zhou
Ranch Hand

Joined: May 04, 2011
Posts: 31

you can use map like this:


I'am a Chinese.I like Java.Hello.
ankit chaubal
Greenhorn

Joined: May 16, 2011
Posts: 3
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

Joined: Mar 13, 2009
Posts: 492

The one that is repeated the most will be the entry with the highest count.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
ankit chaubal
Greenhorn

Joined: May 16, 2011
Posts: 3
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

Joined: Mar 13, 2009
Posts: 492

Ignore the map for a second. How would you find the maximum number in an array?

Hunter
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying the most highly repeated entry in an ArrayList