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

    Exclamation obtaining the most highly repeated entry in an arraylist

    i have written a program which accepts a file from user and data is stored in an ArrayList named "employee". i need to find out the name which is repeated the most number of times. how do i get the most highly repeated name in the arraylist???

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ankit1801 View Post
    i have written a program which accepts a file from user and data is stored in an ArrayList named "employee". i need to find out the name which is repeated the most number of times. how do i get the most highly repeated name in the arraylist???
    Use a Map<String, Integer> where the key is the name and the value the frequency of that name in your input. When it is entirely populated iterate over the Map to find the name (the key) with the largest frequency (the value).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    ankit1801, your duplicate post of the question has been locked. Please continue the discussion here.

    db

  4. #4
    ankit1801 is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Sorry was not able to locate my older thread.

    i have not worked with maps. could you give me a short example or some kind of pseudocode so that i can understand how to implement it in my program?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ankit1801 View Post
    Sorry was not able to locate my older thread.

    i have not worked with maps. could you give me a short example or some kind of pseudocode so that i can understand how to implement it in my program?
    Put your names in the Map<String, Integer> like this:

    Java Code:
    private Map<String, Integer> map= new HashMap<String, Integer>();
    public void add(String key);
       Integer freq= map.get(key);
       if (freq == null)
          freq= Integer.valueOf(0);
       map.put(key, freq+1);
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

Similar Threads

  1. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  2. Obtaining straightline pixels
    By base2coder in forum New To Java
    Replies: 2
    Last Post: 09-10-2009, 01:26 AM
  3. Obtaining relative path outside of executable JAR
    By tjsaker in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2008, 06:48 AM
  4. Replies: 3
    Last Post: 09-27-2008, 04:46 AM
  5. Replies: 1
    Last Post: 11-16-2007, 10:09 AM

Posting Permissions

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