-3

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

Community
  • 1
  • 1
Othman
  • 332
  • 5
  • 19

8 Answers8

3

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
Adam Rabung
  • 5,232
  • 2
  • 27
  • 42
2

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);
    }
}
Bhushan
  • 18,329
  • 31
  • 104
  • 137
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

James
  • 264
  • 1
  • 3
  • 13
1
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);
    }
}
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • As this is homework and the OP has not shown any effort would it not have been better to point him/her in the right direction as opposed to doing it for them? – Darren Burgess Jan 06 '12 at 16:36
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]++;
}
awestover89
  • 1,713
  • 4
  • 21
  • 37
0

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.

Wilmer
  • 1,025
  • 5
  • 9
0
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;
}
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
0
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( " " );
}
Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53