Results 1 to 5 of 5
- 05-16-2011, 06:46 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
finding most highly repeated entry in ArrayList
i have an Arraylist containing names of certain proteins. i need to find which protein is repeated the most number of times and have to print that protein. how do i find it?Someone suggested me to use maps but i have never worked with maps. can it be done using arraylists only? can anybody please provide me the syntax for doing it?
- 05-16-2011, 07:11 AM #2
Sure it can be done with ArrayList but it will involve using nested loops and iterating over the List numerous times. The longer the List gets the more time consuming it becomes. The easiest way to do this is when a new item gets added to the List. At the same time you check if this item is already in the List and increment the count for it. If not create a new count. this is best achieved with a Map as you have already been told. But if you are adverse to it the create a pseudo Map by creating a ItemCount class, create objects and add them to an array.
- 05-16-2011, 07:11 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
A map can contain two types which represent a key and a value. You use a key to look up the value. You may want to consider looping through the array list and heckling two things. If the map doesn't have a key for the current protein it should add a new key value entry. If it does find it you should increment the value.
- 05-16-2011, 09:15 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,412
- Blog Entries
- 7
- Rep Power
- 17
Don't double post; what was wrong with my answer in this thread? Don't try a clumsy solution if Maps can supply an efficient solution.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-16-2011, 09:30 AM #5
Similar Threads
-
obtaining the most highly repeated entry in an arraylist
By ankit1801 in forum New To JavaReplies: 5Last Post: 05-16-2011, 01:44 PM -
removing repeated entries in arraylist
By ankit1801 in forum New To JavaReplies: 1Last Post: 04-15-2011, 06:34 AM -
Finding the most repeated names in a list
By jboy in forum New To JavaReplies: 2Last Post: 09-17-2009, 03:08 PM -
help me in finding the entry points in the source code of java.....
By aks.nitw in forum Advanced JavaReplies: 0Last Post: 11-25-2008, 09:24 AM -
ArrayList problem (finding largest no)
By bugger in forum New To JavaReplies: 3Last Post: 12-12-2007, 12:47 PM


LinkBack URL
About LinkBacks


Bookmarks