0

例如,我有这个数组:

|4758322| 10000 | 5748883 |2754331|

我需要返回数组中出现频率最高的数字。在此示例中,该方法将返回 10000。有人可以帮助我吗?我被卡住了,不知道如何开始。

谢谢 !

4

2 回答 2

3

Since you did not provide code. So I am not going to write it either.
Try doing the following :

For each number in the array get the modulo 10 untill value is zero. you will have an int. So count the number times of each digit appears in the number. Store the highest count. repeat the procedure for other numbers. And compare the counts for each. Finally display the number with highest counts

于 2013-11-13T08:05:58.960 回答
0

我会首先尝试解决问题的简单版本,然后尝试扩展它以解决整个问题。

我会尝试首先从一个整数中获取数字的频率,这几乎可以解决您的其余问题,例如

public static int maxFrequency(int number){
    int[] array = new int[(number)+"".length()];
    int max = 0;
    while(number != 0){
        array[number%10] += 1
        if(max < array[number%10]){
            max = array[number%10];
        }
        number = number/10;
    }
    return max;
}

然后我将使用此方法将其扩展到数组中的所有元素并选择最大的。

int[] a = {4758322, 10000 , 5748883 ,2754331};
    int max = 0;
    int target = 0;

    for(int i : a){

        int maxFrequencyi = maxFrequency(i);
        if(max < maxFrequencyi){
            max = maxFrequencyi;
            target = i;
        }
    }
  return target;
于 2013-11-13T08:53:16.440 回答