如何统计字符串中字符出现的频率?

发布于 2024-11-24 09:22:58 字数 138 浏览 4 评论 0原文

我需要编写某种循环来计算字符串中每个字母的频率。
例如:“aasjjikkk”会计算 2 个“a”、1 个“s”、2 个“j”、1 个“i”、3 个“k”。最终 id 像这些一样最终出现在一个以字符为键、计数为值的映射中。有什么好主意如何做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

一百个冬季 2024-12-01 09:22:59

您可以使用Multiset(来自番石榴)。它会给你每个对象的计数。例如:

Multiset<Character> chars = HashMultiset.create();
for (int i = 0; i < string.length(); i++) {
    chars.add(string.charAt(i));
}

然后对于每个字符,您可以调用 chars.count('a') 并返回出现的次数

You can use a Multiset (from guava). It will give you the count for each object. For example:

Multiset<Character> chars = HashMultiset.create();
for (int i = 0; i < string.length(); i++) {
    chars.add(string.charAt(i));
}

Then for each character you can call chars.count('a') and it returns the number of occurrences

梦行七里 2024-12-01 09:22:59

这是另一种解决方案,尽管它可能很狡猾。

public char getNumChar(String s) {
    char[] c = s.toCharArray();
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    int[] countArray = new int[26];
    for (char x : c) {
        for (int i = 0; i < alphabet.length(); i++) {
            if (alphabet.charAt(i) == x) {
                countArray[i]++;
            }
        }
    }

    java.util.HashMap<Integer, Character> countList = new java.util.HashMap<Integer, Character>();

    for (int i = 0; i < 26; i++) {
        countList.put(countArray[i], alphabet.charAt(i));
    }
    java.util.Arrays.sort(countArray);
    int max = countArray[25];
    return countList.get(max);
}

Here is another solution, dodgy as it may be.

public char getNumChar(String s) {
    char[] c = s.toCharArray();
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    int[] countArray = new int[26];
    for (char x : c) {
        for (int i = 0; i < alphabet.length(); i++) {
            if (alphabet.charAt(i) == x) {
                countArray[i]++;
            }
        }
    }

    java.util.HashMap<Integer, Character> countList = new java.util.HashMap<Integer, Character>();

    for (int i = 0; i < 26; i++) {
        countList.put(countArray[i], alphabet.charAt(i));
    }
    java.util.Arrays.sort(countArray);
    int max = countArray[25];
    return countList.get(max);
}
十秒萌定你 2024-12-01 09:22:59

由于没有 Java 8 解决方案,因此考虑发布一个。此外,这个解决方案比这里提到的其他一些解决方案更加整洁、可读和简洁。

String string = "aasjjikkk";

Map<Character, Long> characterFrequency = string.chars()  // creates an IntStream
    .mapToObj(c -> (char) c) // converts the IntStream to Stream<Character>
    .collect(Collectors.groupingBy(c -> c, Collectors.counting())); // creates a
                                                                    // Map<Character, Long> 
                                                                    // where the Long is
                                                                    // the frequency

Since there was no Java 8 solution, thought of posting one. Also, this solution is much neater, readable and concise than some of the other solutions mentioned here.

String string = "aasjjikkk";

Map<Character, Long> characterFrequency = string.chars()  // creates an IntStream
    .mapToObj(c -> (char) c) // converts the IntStream to Stream<Character>
    .collect(Collectors.groupingBy(c -> c, Collectors.counting())); // creates a
                                                                    // Map<Character, Long> 
                                                                    // where the Long is
                                                                    // the frequency
情绪失控 2024-12-01 09:22:59

嗯,我想到了两种方法,这取决于您的偏好:

  1. 按字符对数组进行排序。然后,计算每个字符就变得微不足道了。但您必须首先复制该数组。

  2. 创建另一个大小为 26 的整数数组(例如 freq),str 是字符数组。

    for(int i = 0; i < str.length; i ++)

    freq[str[i] - 'a'] ++; //假设所有字符均为小写

因此 'a' 的数量将存储在 freq[0] 中,'z' 的数量将存储在 freq[25] 中

Well, two ways come to mind and it depends on your preference:

  1. Sort the array by characters. Then, counting each character becomes trivial. But you will have to make a copy of the array first.

  2. Create another integer array of size 26 (say freq) and str is the array of characters.

    for(int i = 0; i < str.length; i ++)

    freq[str[i] - 'a'] ++; //Assuming all characters are in lower case

So the number of 'a' 's will be stored at freq[0] and the number of 'z' 's will be at freq[25]

〗斷ホ乔殘χμё〖 2024-12-01 09:22:59

这是一个解决方案:

定义您自己的Pair

public class Pair
{
    private char letter;
    private int count;
    public Pair(char letter, int count)
    {
        this.letter = letter;
        this.count= count;
    }
    public char getLetter(){return key;}
    public int getCount(){return count;}
}

然后您可以这样做:

public static Pair countCharFreq(String s)
{
    String temp = s;
    java.util.List<Pair> list = new java.util.ArrayList<Pair>();
    while(temp.length() != 0)
    {
        list.add(new Pair(temp.charAt(0), countOccurrences(temp, temp.charAt(0))));
        temp.replaceAll("[" + temp.charAt(0) +"]","");
    }
}

public static int countOccurrences(String s, char c)
{
    int count = 0;
    for(int i = 0; i < s.length(); i++)
    {
        if(s.charAt(i) == c) count++;
    }
    return count;
}

Here is a solution:

Define your own Pair:

public class Pair
{
    private char letter;
    private int count;
    public Pair(char letter, int count)
    {
        this.letter = letter;
        this.count= count;
    }
    public char getLetter(){return key;}
    public int getCount(){return count;}
}

Then you could do:

public static Pair countCharFreq(String s)
{
    String temp = s;
    java.util.List<Pair> list = new java.util.ArrayList<Pair>();
    while(temp.length() != 0)
    {
        list.add(new Pair(temp.charAt(0), countOccurrences(temp, temp.charAt(0))));
        temp.replaceAll("[" + temp.charAt(0) +"]","");
    }
}

public static int countOccurrences(String s, char c)
{
    int count = 0;
    for(int i = 0; i < s.length(); i++)
    {
        if(s.charAt(i) == c) count++;
    }
    return count;
}
瑾兮 2024-12-01 09:22:59
String s = "aaaabbbbcccddddd";
Map<Character, Integer> map = new HashMap<>();

在Java8中使用一行

s.chars().forEach(e->map.put((char)e, map.getOrDefault((char)e, 0) + 1));
String s = "aaaabbbbcccddddd";
Map<Character, Integer> map = new HashMap<>();

Using one line in Java8

s.chars().forEach(e->map.put((char)e, map.getOrDefault((char)e, 0) + 1));
戒ㄋ 2024-12-01 09:22:59

您可以使用 Eclipse Collections 中的 CharAdapterCharBag 并避免对 CharacterInteger 进行装箱。

CharBag bag = Strings.asChars("aasjjikkk").toBag();

Assert.assertEquals(2, bag.occurrencesOf('a'));
Assert.assertEquals(1, bag.occurrencesOf('s'));
Assert.assertEquals(2, bag.occurrencesOf('j'));
Assert.assertEquals(1, bag.occurrencesOf('i'));
Assert.assertEquals(3, bag.occurrencesOf('k'));

注意:我是 Eclipse Collections 的提交者。

You can use a CharAdapter and a CharBag from Eclipse Collections and avoid boxing to Character and Integer.

CharBag bag = Strings.asChars("aasjjikkk").toBag();

Assert.assertEquals(2, bag.occurrencesOf('a'));
Assert.assertEquals(1, bag.occurrencesOf('s'));
Assert.assertEquals(2, bag.occurrencesOf('j'));
Assert.assertEquals(1, bag.occurrencesOf('i'));
Assert.assertEquals(3, bag.occurrencesOf('k'));

Note: I am a committer for Eclipse Collections.

浮光之海 2024-12-01 09:22:59

还有一种选择,它看起来相当不错。
从 java 8 开始,有新的方法 merge java doc

  public static void main(String[] args) {
    String s = "aaabbbcca";

    Map<Character, Integer> freqMap = new HashMap<>();
    for (int i = 0; i < s.length(); i++) {
        Character c = s.charAt(i);
        freqMap.merge(c, 1, (a, b) -> a + b);
    }
    freqMap.forEach((k, v) -> System.out.println(k + " and " + v));
}

或者使用 ForEach 更干净

       for (Character c : s.toCharArray()) {
            freqMapSecond.merge(c, 1, Integer::sum);
           }

There is one more option and it looks quite nice.
Since java 8 there is new method merge java doc

  public static void main(String[] args) {
    String s = "aaabbbcca";

    Map<Character, Integer> freqMap = new HashMap<>();
    for (int i = 0; i < s.length(); i++) {
        Character c = s.charAt(i);
        freqMap.merge(c, 1, (a, b) -> a + b);
    }
    freqMap.forEach((k, v) -> System.out.println(k + " and " + v));
}

Or even cleaner with ForEach

       for (Character c : s.toCharArray()) {
            freqMapSecond.merge(c, 1, Integer::sum);
           }
自此以后,行同陌路 2024-12-01 09:22:59
package com.rishi.zava;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ZipString {
    public static void main(String arg[]) {
        String input = "aaaajjjgggtttssvvkkllaaiiikk";
        int len = input.length();
        Map<Character, Integer> zip = new HashMap<Character, Integer>();
        for (int j = 0; len > j; j++) {
            int count = 0;
            for (int i = 0; len > i; i++) {
                if (input.charAt(j) == input.charAt(i)) {
                    count++;
                }
            }
            zip.put(input.charAt(j), count);
        }
        StringBuffer myValue = new StringBuffer();
        String myMapKeyValue = "";
        for (Entry<Character, Integer> entry : zip.entrySet()) {
            myMapKeyValue = Character.toString(entry.getKey()).concat(
                    Integer.toString(entry.getValue()));
            myValue.append(myMapKeyValue);
        }
        System.out.println(myValue);
    }
}

输入 = aaaajjjgggtttssvvkkllaaiiikk

输出 = a6s2t3v2g3i3j3k4l2

package com.rishi.zava;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ZipString {
    public static void main(String arg[]) {
        String input = "aaaajjjgggtttssvvkkllaaiiikk";
        int len = input.length();
        Map<Character, Integer> zip = new HashMap<Character, Integer>();
        for (int j = 0; len > j; j++) {
            int count = 0;
            for (int i = 0; len > i; i++) {
                if (input.charAt(j) == input.charAt(i)) {
                    count++;
                }
            }
            zip.put(input.charAt(j), count);
        }
        StringBuffer myValue = new StringBuffer();
        String myMapKeyValue = "";
        for (Entry<Character, Integer> entry : zip.entrySet()) {
            myMapKeyValue = Character.toString(entry.getKey()).concat(
                    Integer.toString(entry.getValue()));
            myValue.append(myMapKeyValue);
        }
        System.out.println(myValue);
    }
}

Input = aaaajjjgggtttssvvkkllaaiiikk

Output = a6s2t3v2g3i3j3k4l2

半窗疏影 2024-12-01 09:22:59

如果这不需要超快,只需创建一个整数数组,每个字母一个整数(只有字母,所以 2*26 整数?或任何可能的二进制数据?)。一次遍历字符串一个字符,获取负责整数的索引(例如,如果只有字母字符,则可以将“A”放在索引 0 处,并通过减去任何“A”到“Z”来获取该索引“A”只是作为如何获得相当快的索引的示例)并增加该索引中的值。

有各种微观优化可以使其更快(如果需要)。

If this does not need to be super-fast just create an array of integers, one integer for each letter (only alphabetic so 2*26 integers? or any binary data possible?). go through the string one char at a time, get the index of the responsible integer (e.g. if you only have alphabetic chars you can have 'A' be at index 0 and get that index by subtracting any 'A' to 'Z' by 'A' just as an example of how you can get reasonably fast indices) and increment the value in that index.

There are various micro-optimizations to make this faster (if necessary).

悍妇囚夫 2024-12-01 09:22:59

您可以使用哈希表,其中每个字符作为键,总计数成为值。

Hashtable<Character,Integer> table = new Hashtable<Character,Integer>();
String str = "aasjjikkk";
for( c in str ) {
    if( table.get(c) == null )
        table.put(c,1);
    else
        table.put(c,table.get(c) + 1);
}

for( elem in table ) {
    println "elem:" + elem;
}

You can use a Hashtable with each character as the key and the total count becomes the value.

Hashtable<Character,Integer> table = new Hashtable<Character,Integer>();
String str = "aasjjikkk";
for( c in str ) {
    if( table.get(c) == null )
        table.put(c,1);
    else
        table.put(c,table.get(c) + 1);
}

for( elem in table ) {
    println "elem:" + elem;
}
你的背包 2024-12-01 09:22:59

这与 xunil154 的答案类似,只不过将字符串制成 char 数组,并使用链接的哈希图来维护字符的插入顺序。

String text = "aasjjikkk";
char[] charArray = text.toCharArray();
Map<Character, Integer> freqList = new LinkedHashMap<Character, Integer>();

        for(char key : charArray) {
            if(freqList.containsKey(key)) {
               freqList.put(key, freqList.get(key) + 1);
            } else
                freqList.put(key, 1);
        }

This is similar to xunil154's answer, except that a string is made a char array and a linked hashmap is used to maintain the insertion order of the characters.

String text = "aasjjikkk";
char[] charArray = text.toCharArray();
Map<Character, Integer> freqList = new LinkedHashMap<Character, Integer>();

        for(char key : charArray) {
            if(freqList.containsKey(key)) {
               freqList.put(key, freqList.get(key) + 1);
            } else
                freqList.put(key, 1);
        }
陈甜 2024-12-01 09:22:59
import java.util.*;
class Charfrequency
{
 public static void main(String a[]){

        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Your String :");
        String s1=sc.nextLine();
        int count,j=1;
        char var='a';
        char ch[]=s1.toCharArray();
        while(j<=26)
        {
           count=0;
                for(int i=0; i<s1.length(); i++)
                {
                    if(ch[i]==var || ch[i]==var-32)
                    {
                        count++;
                    }
                }
                if(count>0){
                System.out.println("Frequency of "+var+" is "+count);
                }
                var++;
                j++;
        }
 }
}
import java.util.*;
class Charfrequency
{
 public static void main(String a[]){

        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Your String :");
        String s1=sc.nextLine();
        int count,j=1;
        char var='a';
        char ch[]=s1.toCharArray();
        while(j<=26)
        {
           count=0;
                for(int i=0; i<s1.length(); i++)
                {
                    if(ch[i]==var || ch[i]==var-32)
                    {
                        count++;
                    }
                }
                if(count>0){
                System.out.println("Frequency of "+var+" is "+count);
                }
                var++;
                j++;
        }
 }
}
旧时浪漫 2024-12-01 09:22:59

使用 HashMap 的简短可能代码。 (没有强力的线路保存)

private static Map<Character, Integer> findCharacterFrequency(String str) {

        Map<Character, Integer> map = new HashMap<>();

        for (char ch : str.toCharArray()) {
            /* Using getOrDefault(), since Java1.8 */
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        return map;
    }

The shorted possible code using a HashMap. (With no forceful line saves)

private static Map<Character, Integer> findCharacterFrequency(String str) {

        Map<Character, Integer> map = new HashMap<>();

        for (char ch : str.toCharArray()) {
            /* Using getOrDefault(), since Java1.8 */
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        return map;
    }
徒留西风 2024-12-01 09:22:59

请尝试下面给出的代码,希望对您有帮助,

import java.util.Scanner;

class String55 {
    public static int frequency(String s1,String s2)
    {

        int count=0;
        char ch[]=s1.toCharArray();
        char ch1[]=s2.toCharArray();
        for (int i=0;i<ch.length-1; i++)
        {



                int k=i;

                int j1=i+1;
                int j=0;
                int j11=j;
                int j2=j+1;
                {
                    while(k<ch.length && j11<ch1.length && ch[k]==ch1[j11])
                    {
                    k++;
                    j11++;

                    }


                int l=k+j1;
                int m=j11+j2;
                if( l== m)
                {
                    count=1;
                    count++;
                }





        }
        }
        return count;


    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the pattern");
        String s1=sc.next();
            System.out.println("enter the String");
            String s2=sc.next();
            int res=frequency(s1, s2);
            System.out.println("FREQUENCY==" +res);

    }
}

示例输出:
输入图案
男人
输入字符串
迪曼曼
FREQUENCY==2

谢谢。编码愉快。

Please try the given code below, hope it will helpful to you,

import java.util.Scanner;

class String55 {
    public static int frequency(String s1,String s2)
    {

        int count=0;
        char ch[]=s1.toCharArray();
        char ch1[]=s2.toCharArray();
        for (int i=0;i<ch.length-1; i++)
        {



                int k=i;

                int j1=i+1;
                int j=0;
                int j11=j;
                int j2=j+1;
                {
                    while(k<ch.length && j11<ch1.length && ch[k]==ch1[j11])
                    {
                    k++;
                    j11++;

                    }


                int l=k+j1;
                int m=j11+j2;
                if( l== m)
                {
                    count=1;
                    count++;
                }





        }
        }
        return count;


    }
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the pattern");
        String s1=sc.next();
            System.out.println("enter the String");
            String s2=sc.next();
            int res=frequency(s1, s2);
            System.out.println("FREQUENCY==" +res);

    }
}

SAMPLE OUTPUT:
enter the pattern
man
enter the String
dhimanman
FREQUENCY==2

Thank-you.Happy coding.

念﹏祤嫣 2024-12-01 09:22:59
package com.dipu.string;

import java.util.HashMap;
import java.util.Map;

public class RepetativeCharInString {
    public static void main(String[] args) {
        String data = "aaabbbcccdddffffrss";
        char[] charArray = data.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        for (char c : charArray) {
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }
        System.out.println(map);

    }
}
package com.dipu.string;

import java.util.HashMap;
import java.util.Map;

public class RepetativeCharInString {
    public static void main(String[] args) {
        String data = "aaabbbcccdddffffrss";
        char[] charArray = data.toCharArray();
        Map<Character, Integer> map = new HashMap<>();
        for (char c : charArray) {
            if (map.containsKey(c)) {
                map.put(c, map.get(c) + 1);
            } else {
                map.put(c, 1);
            }
        }
        System.out.println(map);

    }
}
难如初 2024-12-01 09:22:59
*import java.util.ArrayList;
import java.util.Collections;

public class Freq {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String temp="zsaaqaaaaaaaabbbbbcc";
    List<String> temp1= new ArrayList<String> ();
    ArrayList<Integer>freq=new ArrayList<Integer>();
    for(int i=0;i<temp.length()-1;i++)
    {       
        temp1.add(Character.toString(temp.charAt(i)));      
    }
    Set<String> uniqset=new HashSet<String>(temp1);
    for(String s:uniqset)
    {
        freq.add(Collections.frequency(temp1, s));
        System.out.println(s+" -->>"+Collections.frequency(temp1, s));
    }
    }

}
           ------Output-------
       a -->>10
       b -->>5
       c -->>1
       q -->>1
       s -->>1
       z -->>1

使用集合频率方法来统计 char* 的频率

*import java.util.ArrayList;
import java.util.Collections;

public class Freq {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String temp="zsaaqaaaaaaaabbbbbcc";
    List<String> temp1= new ArrayList<String> ();
    ArrayList<Integer>freq=new ArrayList<Integer>();
    for(int i=0;i<temp.length()-1;i++)
    {       
        temp1.add(Character.toString(temp.charAt(i)));      
    }
    Set<String> uniqset=new HashSet<String>(temp1);
    for(String s:uniqset)
    {
        freq.add(Collections.frequency(temp1, s));
        System.out.println(s+" -->>"+Collections.frequency(temp1, s));
    }
    }

}
           ------Output-------
       a -->>10
       b -->>5
       c -->>1
       q -->>1
       s -->>1
       z -->>1

Use collections frequency method to count frequency of char*

债姬 2024-12-01 09:22:59

呃。您不认为这是最简单的解决方案吗?

    char inputChar = '|';
    int freq = "|fd|fdfd|f dfd|fd".replaceAll("[^" + inputChar +"]", "").length();
    System.out.println("freq " + freq);

Uffh. Don't you think this is the simplest solution?

    char inputChar = '|';
    int freq = "|fd|fdfd|f dfd|fd".replaceAll("[^" + inputChar +"]", "").length();
    System.out.println("freq " + freq);
尬尬 2024-12-01 09:22:59

为此,我们可以使用 Collections 类的频率方法。
将字符串拆分为字符串数组。使用HashSet删除重复项并使用Collections的频率方法检查HashSet中每个对象的频率

void usingCollections(){

  String input = "cuttack";

  String [] stringArray = input.split("");

  Set<String> s = new HashSet(Arrays.asList(stringArray));

  for(String abc : s){

    System.out.println (abc + ":"+Collections.frequency(Arrays.asList(stringArray),abc));

  }
}

We can use frequency method of Collections class for this.
Split the string into string array. Use HashSet to remove duplicates and check frequency of each object in HashSet using frequency method of Collections

void usingCollections(){

  String input = "cuttack";

  String [] stringArray = input.split("");

  Set<String> s = new HashSet(Arrays.asList(stringArray));

  for(String abc : s){

    System.out.println (abc + ":"+Collections.frequency(Arrays.asList(stringArray),abc));

  }
}
只有一腔孤勇 2024-12-01 09:22:59

这是计算字符串中字符频率的更有效方法

public class demo {
    public static void main(String[] args) {
        String s = "babdcwertyuiuygf";
        Map<Character, Integer> map = new TreeMap<>();
        s.chars().forEach(e->map.put((char)e, map.getOrDefault((char)e, 0) + 1));
        StringBuffer myValue = new StringBuffer();
        String myMapKeyValue = "";
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            myMapKeyValue = Character.toString(entry.getKey()).concat(
                Integer.toString(entry.getValue()));
            myValue.append(myMapKeyValue);
        }
        System.out.println(myValue);
    }
}

This is more Effective way to count frequency of characters in a string

public class demo {
    public static void main(String[] args) {
        String s = "babdcwertyuiuygf";
        Map<Character, Integer> map = new TreeMap<>();
        s.chars().forEach(e->map.put((char)e, map.getOrDefault((char)e, 0) + 1));
        StringBuffer myValue = new StringBuffer();
        String myMapKeyValue = "";
        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            myMapKeyValue = Character.toString(entry.getKey()).concat(
                Integer.toString(entry.getValue()));
            myValue.append(myMapKeyValue);
        }
        System.out.println(myValue);
    }
}
不回头走下去 2024-12-01 09:22:59

另一种使用地图合并方法的方法

   Map<Character, Integer> map = new HashMap<>();
   String s = "aasjjikkk";
    for (int i = 0; i < s.length(); i++) {
        map.merge(s.charAt(i), 1, (l, r) -> l + r);

Another way using map merge method

   Map<Character, Integer> map = new HashMap<>();
   String s = "aasjjikkk";
    for (int i = 0; i < s.length(); i++) {
        map.merge(s.charAt(i), 1, (l, r) -> l + r);
荒芜了季节 2024-12-01 09:22:59
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;


public class FrequenceyOfCharacters {

    public static void main(String[] args) {
        System.out.println("Please enter the string to count each character frequencey: ");
        Scanner sc=new  Scanner(System.in);
        String s =sc.nextLine();
        String input = s.replaceAll("\\s",""); // To remove space.
        frequenceyCount(input);
        
        
    }

    private static void frequenceyCount(String input) {
        
        Map<Character,Integer> hashCount=new HashMap<>();
        Character c;
        for(int i=0; i<input.length();i++)
        {
           c =input.charAt(i);
           if(hashCount.get(c)!=null){
               hashCount.put(c, hashCount.get(c)+1);
           }else{
               hashCount.put(c, 1);
           }
        }
        Iterator it = hashCount.entrySet().iterator();
        System.out.println("char : frequency");
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            System.out.println(pairs.getKey() + " : " + pairs.getValue());
            it.remove(); 
        }
        
    }

}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;


public class FrequenceyOfCharacters {

    public static void main(String[] args) {
        System.out.println("Please enter the string to count each character frequencey: ");
        Scanner sc=new  Scanner(System.in);
        String s =sc.nextLine();
        String input = s.replaceAll("\\s",""); // To remove space.
        frequenceyCount(input);
        
        
    }

    private static void frequenceyCount(String input) {
        
        Map<Character,Integer> hashCount=new HashMap<>();
        Character c;
        for(int i=0; i<input.length();i++)
        {
           c =input.charAt(i);
           if(hashCount.get(c)!=null){
               hashCount.put(c, hashCount.get(c)+1);
           }else{
               hashCount.put(c, 1);
           }
        }
        Iterator it = hashCount.entrySet().iterator();
        System.out.println("char : frequency");
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            System.out.println(pairs.getKey() + " : " + pairs.getValue());
            it.remove(); 
        }
        
    }

}
梦行七里 2024-12-01 09:22:59
import java.io.FileInputStream;
import java.util.HashSet;
import java.util.Iterator;
public class CountFrequencyOfCharater {
public static void main(String args[]) throws Exception
{
    HashSet hs=new HashSet();
    String str="hey how are you?";
    char arr[]=new char[str.length()];
    for(int i=0;i<str.length();i++)
    {
        arr[i]=str.charAt(i);
    }
    for(int j=0;j<str.length();j++)
    {
        int c=0;
        for(int k=0;k<str.length();k++)
        {
            if(arr[j]==arr[k])
            c++;
        }
        hs.add(arr[j]+"="+c+",");
    }
        Iterator it=hs.iterator();
        while(it.hasNext())
        {
             System.out.print(it.next());
        }
  }
}
import java.io.FileInputStream;
import java.util.HashSet;
import java.util.Iterator;
public class CountFrequencyOfCharater {
public static void main(String args[]) throws Exception
{
    HashSet hs=new HashSet();
    String str="hey how are you?";
    char arr[]=new char[str.length()];
    for(int i=0;i<str.length();i++)
    {
        arr[i]=str.charAt(i);
    }
    for(int j=0;j<str.length();j++)
    {
        int c=0;
        for(int k=0;k<str.length();k++)
        {
            if(arr[j]==arr[k])
            c++;
        }
        hs.add(arr[j]+"="+c+",");
    }
        Iterator it=hs.iterator();
        while(it.hasNext())
        {
             System.out.print(it.next());
        }
  }
}
甜点 2024-12-01 09:22:59

#来自C语言

 #include<stdio.h>`
 #include <string.h>`
  int main()
{
    char s[1000];  
    int  i,j,k,count=0,n;
    printf("Enter  the string : ");
    gets(s);
    for(j=0;s[j];j++);
    n=j; 
    printf(" frequency count character in string:\n");
    for(i=0;i<n;i++)  
    {
        count=1;
        if(s[i])
        {
        
          for(j=i+1;j<n;j++)  
          {   
            
            if(s[i]==s[j])
            {
                 count++;
                 s[j]='\0';
            }
          }  
          printf(" '%c' = %d \n",s[i],count);
       }
    } 
    return 0;
}

#From C language

 #include<stdio.h>`
 #include <string.h>`
  int main()
{
    char s[1000];  
    int  i,j,k,count=0,n;
    printf("Enter  the string : ");
    gets(s);
    for(j=0;s[j];j++);
    n=j; 
    printf(" frequency count character in string:\n");
    for(i=0;i<n;i++)  
    {
        count=1;
        if(s[i])
        {
        
          for(j=i+1;j<n;j++)  
          {   
            
            if(s[i]==s[j])
            {
                 count++;
                 s[j]='\0';
            }
          }  
          printf(" '%c' = %d \n",s[i],count);
       }
    } 
    return 0;
}
请止步禁区 2024-12-01 09:22:58

您可以使用 java Map 将 char 映射到 int。然后,您可以迭代字符串中的字符并检查它们是否已添加到映射中,如果有,则可以增加其值。

例如:

Map<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, val + 1);
    }
    else {
       map.put(c, 1);
   }
}

最后,您将获得遇到的所有字符的计数,并且可以从中提取它们的频率。

或者,您可以使用 Bozho 的解决方案,即使用 Multiset 并计算总出现次数。

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:

Map<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, val + 1);
    }
    else {
       map.put(c, 1);
   }
}

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.

雨巷深深 2024-12-01 09:22:58

从 JDK-8 开始使用流 API:

Map<Character, Long> frequency =
            str.chars()
               .mapToObj(c -> (char)c)
               .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

或者如果您希望将值作为 Integer

Map<Character, Integer> frequency =
            str.chars()
               .mapToObj(c -> (char)c)
               .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(c -> 1)));

另一种变体:

Map<Character, Integer> frequency = 
            str.chars()
               .mapToObj(c -> (char)c)
               .collect(Collectors.toMap(Function.identity(), c -> 1, Math::addExact));

Using the stream API as of JDK-8:

Map<Character, Long> frequency =
            str.chars()
               .mapToObj(c -> (char)c)
               .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

or if you want the values as Integers:

Map<Character, Integer> frequency =
            str.chars()
               .mapToObj(c -> (char)c)
               .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(c -> 1)));

Another variant:

Map<Character, Integer> frequency = 
            str.chars()
               .mapToObj(c -> (char)c)
               .collect(Collectors.toMap(Function.identity(), c -> 1, Math::addExact));
初懵 2024-12-01 09:22:58

执行此操作的一种简洁方法是:

Map<Character,Integer> frequencies = new HashMap<>();
for (char ch : input.toCharArray())
   frequencies.put(ch, frequencies.getOrDefault(ch, 0) + 1);

我们使用 for-each 循环遍历每个字符。如果 key 存在,则 getOrDefault 方法获取值或返回(默认)其第二个参数。


或者,可以使用merge方法;它可以方便地采用 remappingFunction 参数。在本例中,使用 Integer::sum 函数。

Map<Character,Integer> frequencies = new HashMap<>();
for (char ch : input.toCharArray())
   frequencies.merge(ch, 1, Integer::sum);

A concise way to do this is:

Map<Character,Integer> frequencies = new HashMap<>();
for (char ch : input.toCharArray())
   frequencies.put(ch, frequencies.getOrDefault(ch, 0) + 1);

We use a for-each to loop through every character. The getOrDefault method gets the value, if key is present or returns (as default) its second argument.


Alternatively, the merge method can be used; which conveniently takes a remappingFunction parameter. In this case the Integer::sum function is used.

Map<Character,Integer> frequencies = new HashMap<>();
for (char ch : input.toCharArray())
   frequencies.merge(ch, 1, Integer::sum);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文