Skip to main content

APIzation: Replication Package

[Q#6712587][A#6712708] How to count frequency of characters in a string?

https://stackoverflow.com/q/6712587

I need to write some kind of loop that can count the frequency of each letter in a string. For example: "aasjjikkk" would count 2 'a', 1 's', 2 'j', 1 'i', 3 'k'. Ultimately id like these to end up in a map with the character as the key and the count as the value. Any good idea how to do this?

Answer

https://stackoverflow.com/a/6712708

You can use a java Map and map a char to an int. You can then iterate over the characters in the string and check if they have been added to the map, if they have, you can then increment its value. For example: At the end you will have a count of all the characters you encountered and you can extract their frequencies from that. Alternatively, you can use Bozho's solution of using a Multiset and counting the total occurences.

APIzation

HashMap<Character, Integer> map = new HashMap<Character, Integer>();
String s = "aasjjikkk";
for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    Integer val = map.get(c);
    if (val != null) {
        map.put(c, new Integer(val + 1));
    }
    else {
       map.put(c, 1);
   }
}
package com.stackoverflow.api;

import java.util.HashMap;

public class Human6712708 {

  public static HashMap countFrequencyOfCharacters() {
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    String s = "aasjjikkk";
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      Integer val = map.get(c);
      if (val != null) {
        map.put(c, new Integer(val + 1));
      } else {
        map.put(c, 1);
      }
    }
    return map;
  }
}

package com.stackoverflow.api;

import java.util.HashMap;

/**
 * How to count frequency of characters in a string?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/6712708">https://stackoverflow.com/a/6712708</a>
 */
public class APIzator6712708 {

  public static void countFrequency(String s) throws Exception {
    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      Integer val = map.get(c);
      if (val != null) {
        map.put(c, new Integer(val + 1));
      } else {
        map.put(c, 1);
      }
    }
  }
}