Java 如何找到整数中重复次数最多的最高数

Java 如何找到整数中重复次数最多的最高数,java,Java,例如,如果 整数=30530 它必须返回3` 这是我尝试过的,但我想不起来了,我不知道我在哪里丢失了它,如果有其他方法可以不用转换成字符串,我也会很感激 public static int maharishi(int functionNum){ String num = Integer.toString(functionNum); int length = num.length(); int count = 0; int tempCount = 0; int charL

例如,如果 整数=30530

它必须返回3`

这是我尝试过的,但我想不起来了,我不知道我在哪里丢失了它,如果有其他方法可以不用转换成字符串,我也会很感激

public static int maharishi(int functionNum){
    String num = Integer.toString(functionNum);
  int length = num.length();
  int count = 0;
  int tempCount =  0;
  int charLetter = 0;
    for(int i = 1; i < length; i++ ){
        for(int j = 1; j < length; j++){
            if(i==1 && j!=1 ){
                if(num.charAt(i) == num.charAt(j)){
                    tempCount++;
                    if(tempCount > count){
                        count = tempCount;
                        charLetter = i;
                    }
                }
            }
        }
    }
    char highestChar = num.charAt(charLetter);
    int change = Integer.parseInt(String.valueOf(highestChar));
    return change;

}
公共静态int-maharishi(int-functionNum){
字符串num=Integer.toString(functionNum);
int length=num.length();
整数计数=0;
int tempCount=0;
int charLetter=0;
for(int i=1;i计数){
计数=临时计数;
charLetter=i;
}
}
}
}
}
char highestChar=num.charAt(charLetter);
int change=Integer.parseInt(String.valueOf(highestChar));
回报变化;
}
  • 中间的
    如果
    条件make all失败,则仅当
    i
    为0时才计数,因此不需要查看其他值
  • 如果在内部循环之后进行比较,则不需要检查每个值
  • 内部
    循环应该从
    i+1开始
    只读取下一个字符
  • tempCount
    应在每一轮外部循环的
    0
    处重新初始化
您可以使用
地图

公共静态int maharishiMaheshYogi(int functionNum){
//将数字转换为字符串
字符串num=Integer.toString(functionNum);
//创建一个映射,在其中存储每个字符计数
最终映射计数=新HashMap();
//迭代此字符串的每个字符
最终整数长度=数值长度();
for(int i=0;imaxEntry.getValue()){
maxEntry=entry;
}
}
返回Integer.parseInt(maxEntry.getKey().toString());
}

如果您不想转换为字符串,最简单的方法是使用“助手数组”来保存从0到9的每个可能数字的计数

public static int maharishi(int functionNum) {
    int [] counts = new int[10];

    //count all the digits in the number:
    while (functionNum > 0) {
        counts[functionNum % 10] += 1;
        functionNum /= 10;
    }

    int record = -1;
    int mostRepeated = -1;

    //find higest most repeated digit
    for (int i = 0; i < counts.length; i++) {
        if (counts[i] >= record) {
            record = counts[i]; //how many times this digit is in the number
            mostRepeated = i; //what digit it is
    }

    return mostRepeated;
}
公共静态int-maharishi(int-functionNum){
int[]计数=新的int[10];
//计算数字中的所有数字:
while(functionNum>0){
计数[functionNum%10]+=1;
functionNum/=10;
}
int记录=-1;
int mostRepeated=-1;
//查找最高重复数
for(int i=0;i=记录){
record=counts[i];//这个数字是数字的多少倍
mostRepeated=i;//它是什么数字
}
返回次数最多;
}
它所做的是:

  • 对数字中的每个数字进行计数。将数字除以模数(余数)进行分隔。
    若要在不转换为字符串的情况下将数字与数字分开,只需获得除10的余数
  • 运行从0到9的计数,并存储哪个计数最大。返回该计数所属的数字(索引),这就是您的答案
  • 您可以在没有阵列的情况下执行此操作,但需要更多循环。

    用模和除法分隔数字的原理是一样的。

    嵌套循环让我感到惊讶。为什么不只计算0、1、2、.9?如何,启发我你是否知道数组?你可以有一个包含10个条目的数组,并使用每个数字作为索引来增加相应的条目….hmm,然后在数组上循环以查找最高计数……也许你的方法毕竟没那么糟糕。;-)要获得低阶数字,请使用
    n%10
    。然后使用
    n=n/10
    将数字一位右移。谢谢它能起作用,我正在试着理解我所有的错误,有没有办法不将其转换为字符串就能做到这一点?@abelkibee不太好,这是一个好方法轻松访问不同元素的方法;)很好…谢谢Borois也很有用…谢谢这也很有用…非常感谢
    public static int maharishi(int functionNum) {
        String l = Arrays.stream(Integer.toString(functionNum).split(""))
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .max(Map.Entry.comparingByValue())
                .orElseThrow()
                .getKey();
        return Integer.parseInt(l);
    }
    
    public static int maharishi(int functionNum) {
        int [] counts = new int[10];
    
        //count all the digits in the number:
        while (functionNum > 0) {
            counts[functionNum % 10] += 1;
            functionNum /= 10;
        }
    
        int record = -1;
        int mostRepeated = -1;
    
        //find higest most repeated digit
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] >= record) {
                record = counts[i]; //how many times this digit is in the number
                mostRepeated = i; //what digit it is
        }
    
        return mostRepeated;
    }