Counting char Occurences in ArrayList

M

mnml

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
 
M

mnml

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;
}

}
 
R

Richard Reynolds

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.");
}
}
}
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top