Possible Duplicate:
Count occurrences of each unique character
How can I get the number of occurrence of each char in a string? for example:
"stackoverflow": s:1 t:1 a:1 c:1 k:1 o:2 v:1 e:1 r:1 f:1 l:1 o:1 w:1
Possible Duplicate:
Count occurrences of each unique character
How can I get the number of occurrence of each char in a string? for example:
"stackoverflow": s:1 t:1 a:1 c:1 k:1 o:2 v:1 e:1 r:1 f:1 l:1 o:1 w:1
Guava to the rescue! I can't believe it, this is a java one-liner!
Multiset<Character> distintCharsAndCount = HashMultiset.create(
Chars.asList("ssssssssstackoverflow".toCharArray())
);
Chars.asList helps us convert the string to a real collection of non-primitives. MultiSets are the perfect structure for what you're after: it's a Set that keep distinct elements, but also keeps the number of occurances in the set.
Access it this way:
int sCount = distintCharsAndCount.count('s'); //sets it to 9
Create HashMap with keys as a Character and Integer count as value.
HashMap<Character, Integer> hm = new HashMap<Character, Integer>()
for (int i = 0; i < str.length; i++) {
if (hm.get(str.charAt(i))) {
int temp = hm.get(str.charAt(i));
hm.put(str.charAt(i), ++temp);
} else {
hm.put(str.charAt(i), 1);
}
}
I would suggest using a map from character to count and iterating over the character array adding to the map or the count as you go along
Map<Character, Integer> chars = new HashMap<Character, Integer>();
for (int i = 0; i < str.length; i++) {
char c = str.charAt(i);
Integer count = chars.get(c);
if (count == null) {
chars.put(c, 1);
} else {
chars.put(c, count + 1);
}
}
A simple but intuitive way that I have done this in the past is to cast the chars as ints and then just use an array. Using a Map is a better approach, but this sounds like a homework problem and using casting and an array is a bit more logical (imo) for a beginner.
int counts[] = new int[26];
String s = 'stackoverflow';
s = s.toLowerCase();
for (int i = 0; i < s.length(); i++) {
int val = (int)(s.charAt(i)) - 97;
counts[val]++;
}
You could parse the string and create a Map<Character, Integer>
so that the integer (value in the map) represents how many times the character c (the key in the map) appears in the string.
public static Map<Character, Integer> count(String s) {
Map<Character, Integer> result = new HashMap<Character,Integer>();
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
Integer n = result.get(c);
result.put(c, n == null ? 1 : n + 1);
}
return result;
}
String input = "stackoverflow";
// desired output: s:1 t:1 a:1 c:1 k:1 o:2 v:1 e:1 r:1 f:1 l:1 o:1 w:1
Map<Character,Integer> counts = new HashMap<Character, Integer>
( Math.min(input.length(),Character.MAX_VALUE) / 2 );
char ch;
for (int i=0; i<input.length(); ++i)
{
ch = input.charAt(i);
if ( !counts.containsKey(ch)) { counts.put(ch,0); }
counts.put(ch, counts.get(ch)+1 );
}
for (Map.Entry<Character,Integer> entry : counts.entrySet() )
{
out.print( entry.getKey() );
out.print( ":" );
out.print( entry.getValue());
out.print( " " );
}