Results 1 to 5 of 5
  1. #1
    ankit1801 is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Unhappy 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?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ankit1801 View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,248
    Rep Power
    19

    Default

    ankit1801, please us one thread per question.

    Locking this duplicate.

    db

Similar Threads

  1. Replies: 5
    Last Post: 05-16-2011, 01:44 PM
  2. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  3. Finding the most repeated names in a list
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 03:08 PM
  4. Replies: 0
    Last Post: 11-25-2008, 09:24 AM
  5. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •