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 - 12 Mar 2008 21:24 GMT
I can also use a Stringbuffer to store the char
> 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 - 12 Mar 2008 22:32 GMT
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;
}
}
> I can also use a Stringbuffer to store the char
>
[quoted text clipped - 4 lines]
> > occurs the most.
> > Thanks
Richard Reynolds - 12 Mar 2008 22:38 GMT
> 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.");
}
}
}
Roedy Green - 14 Mar 2008 04:59 GMT
On Wed, 12 Mar 2008 21:38:10 GMT, Richard Reynolds
<richiereynolds@ntlworld.com> 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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 14 Mar 2008 04:57 GMT
>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.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com