Counting char Occurences in ArrayList

Discussion in 'Java' started by mnml, Mar 12, 2008.

  1. mnml

    mnml Guest

    Hi,
    I have some char stored in an ArrayList, I would like to know if there
    is a way to
    count the occurences of each of these char and return the one that
    occurs the most.
    Thanks
     
    mnml, Mar 12, 2008
    #1
    1. Advertising

  2. mnml

    mnml Guest

    I can also use a Stringbuffer to store the char

    mnml wrote:
    > Hi,
    > I have some char stored in an ArrayList, I would like to know if there
    > is a way to
    > count the occurences of each of these char and return the one that
    > occurs the most.
    > Thanks
     
    mnml, Mar 12, 2008
    #2
    1. Advertising

  3. mnml

    mnml Guest

    I think I have solved it:

    public class Probe {
    private static char out;

    public static char find(StringBuffer proba) {
    int occurence=0;
    //we get each char in the buffer
    for (int i = 0; i < proba.length(); i++) {
    int o = 0;
    char c = proba.charAt(i);
    //for each char in the buffer we check how many
    // time they show up
    for (int x = 0; x < proba.length(); x++)
    {
    if (proba.charAt(i) == c)
    o++;
    //If le char we are checking shows up more
    //than our previous records we keep it as a
    //new record
    if (c > occurence) {
    occurence = o;
    out = c;
    }
    }

    }

    return out;
    }

    }

    On Mar 12, 8:24 pm, mnml <> wrote:
    > I can also use a Stringbuffer to store the char
    >
    > mnml wrote:
    > > Hi,
    > > I have some char stored in an ArrayList, I would like to know if there
    > > is a way to
    > > count the occurences of each of these char and return the one that
    > > occurs the most.
    > > Thanks
     
    mnml, Mar 12, 2008
    #3
  4. On Wed, 12 Mar 2008 12:55:33 -0700 (PDT), mnml wrote:

    > Hi,
    > I have some char stored in an ArrayList, I would like to know if there
    > is a way to
    > count the occurences of each of these char and return the one that
    > occurs the most.
    > Thanks


    lots of ways, here's one that's easy to understand -

    import java.util.*;

    public class Oi
    {
    public static void main(String[] args)
    {
    List<Character> l = new ArrayList<Character>();
    l.add('c');
    l.add('l');
    l.add('h');
    l.add('l');

    if(!l.isEmpty())
    {
    int maxOccurrences = 0;
    char mode = '0';

    Map<Character, Integer> m = new HashMap<Character, Integer>();

    for(char c : l)
    {
    m.put(c, m.get(c) == null ? 1 : m.get(c) + 1);
    }

    for(char c : m.keySet())
    {
    if(m.get(c) > maxOccurrences)
    {
    maxOccurrences = m.get(c);
    mode = c;
    }
    System.out.println(c + ":" + m.get(c));
    }
    System.out.println("Most frequent was " + mode +
    " with " + maxOccurrences + " occurrences.");
    }
    }
    }
     
    Richard Reynolds, Mar 12, 2008
    #4
  5. mnml

    Roedy Green Guest

    On Wed, 12 Mar 2008 12:55:33 -0700 (PDT), mnml <>
    wrote, quoted or indirectly quoted someone who said :

    >Hi,
    >I have some char stored in an ArrayList, I would like to know if there
    >is a way to
    >count the occurences of each of these char and return the one that
    >occurs the most.


    There are several of ways of tackling that. The fastest would be to
    have a an int[ 64*1024] where you index by char and count up how often
    each char occurs. Then you scan the array looking for the biggest
    number.

    That requires a fairly huge array most of which is empty.

    Another way would be to create a HashMap where you look up by char to
    get the count. You then only have entries for chars that occur. The
    lookup is slower.

    Another way would be to collect all the characters to be tested into
    an array and sort them. You can then count how many of each char
    appears and keep track of the best so far, replacing it if you find a
    better one.

    see http://mindprod.com/jgloss/biggest.html
    for how to find the biggest in an array.


    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 14, 2008
    #5
  6. mnml

    Roedy Green Guest

    On Wed, 12 Mar 2008 21:38:10 GMT, Richard Reynolds
    <> wrote, quoted or indirectly quoted
    someone who said :

    >lots of ways, here's one that's easy to understand -


    That sounded like a homework assignment. You rob someone of their
    education if you have them a complete solution.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Mar 14, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,772
    Natty Gur
    Aug 19, 2003
  2. Travers Naran
    Replies:
    4
    Views:
    393
    John Machin
    May 25, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,102
    Ian Collins
    May 9, 2006
  4. jesse
    Replies:
    2
    Views:
    101
    -berlin.de
    Mar 14, 2007
  5. Bart Van der Donck

    Replace all occurences of a char, except the first

    Bart Van der Donck, Sep 20, 2008, in forum: Perl Misc
    Replies:
    6
    Views:
    175
    Tim Greer
    Sep 20, 2008
Loading...

Share This Page