Java program to find the character that appears the most number of times in a String?

前端 未结 8 999
慢半拍i
慢半拍i 2020-12-22 13:03

So here\'s the code I\'ve got so far...

import java.util.Scanner;
class count{
    public static void main(String args[]){
        Scanner s=new Scanner(Syst         


        
8条回答
  •  盖世英雄少女心
    2020-12-22 13:20

    the variable count needs the value to be reset after every iteration of k so it will look like this

        for(int j= 0;j<=len-1;j++){
            for(int k=0;k<=len-1;k++){
                if(ch[j]==ch[k]){
                    arr[j]= count++;
                }
            }count=1;
        }
    

    moreover when you are trying to find the maximum in the array it would be easier and correct in this.

        int max=0;
        for(int z=1;z<=len-1;z++){
            if(arr[z]>arr[max])
                max=z;
        }
    

    what you are doing (rather what it would have done had count been reset) is storing the occurrence count of first character and then comparing it with the no. of occurrences of last character and if more then last character occurrences with itself.

提交回复
热议问题