Java Meistgenutzten Buchstaben eines Strings herausfinden

Erste Frage Aufrufe: 1323     Aktiv: 17.11.2020 um 16:25

1

Hallo, ich suche eine Methode für Java, die einen String als Parameter hat und mit der ich herausfinden kann, welcher Buchstabe am häufigsten verwendet wurde. Danke im Voraus!

Diese Frage melden
gefragt

 
Kommentar schreiben
1 Antwort
1

public static char getMax(String s) {

char maxappearchar = ' ';
int counter = 0;
int[] ascii_count = new int[128];  // fast path for ASCII
HashMap<Character,Integer> nonascii_count = new HashMap<Character,Integer>();

for (int i = 0 ; i < s.length() ; i++)
{
    char ch = s.charAt(i);  // This does appear to be the recommended way to iterate over a String
    // alternatively, iterate over 32bit Unicode codepoints, not UTF-16 chars, if that matters.
    if (ch < 128) {
        ascii_count[ch]++;
    } else {
        // some code to set or increment the nonascii_count[ch];
    }
}

// loop over ascii_count and find the highest element
// loop over the keys in nonascii_count, and see if any of them are even higher.
return maxappearchar;

}

aus https://stackoverflow.com/questions/31990103/more-effective-method-for-finding-the-most-common-character-in-a-string

Diese Antwort melden
geantwortet

 

Kommentar schreiben