【问题标题】:Finding the mode of an array method (java) [duplicate]查找数组方法的模式(java)[重复]
【发布时间】:2014-08-22 17:14:34
【问题描述】:

给定一个数字数组,我必须编写方法来查找平均值、中位数、众数等。我已经解决了其他所有问题,但是模式给了我一个地狱般的时间,不知道为什么。

数组为 22, 26, 45, 46, 49, 55, 57, 63, 63, 65, 66, 67, 67, 68, 68, 69, 70, 71, 72, 73, 75, 76, 76、77、77、78、78、78、79、82、82、83、84、85、87、88、88、89、89、91、92、98、99

它返回 22,而答案应该是 78。

这是我的代码:

public static int mode(int[] array){
    int[] countArray = new int[101];

    //counts each number
    for(int i = 0; i < array.length; i++){
      countArray[array[i]]++;
    }// end for loop

    int mode = array[0], modeIndex = 0;

    //finds which number occurs the most
    System.out.println(Arrays.toString(countArray));
    for(int i = 1; i < array.length; i++){
      if(countArray[i] > mode){

        mode = countArray[i];
        modeIndex = i;
        System.out.println(mode + " " + modeIndex);
      }// end if
    }// end for loop

    return modeIndex;
}// end method mode

【问题讨论】:

  • 有不少地方出错了,尝试调试你的代码,单步调试,问题应该会很明显。
  • this answer 就是你要找的东西

标签: java


【解决方案1】:

你有一个bug,需要修改的两行是:

if(countArray[i] > mode){
    mode = countArray[i];

到:

if(countArray[array[i]] > mode){
    mode = array[i];

【讨论】:

  • 谢谢,实际上我在发帖前花了大约一个小时来解决这个问题,然后几乎立刻就明白了。很抱歉没有回来让大家知道。不过还是谢谢。
【解决方案2】:

你的错误是:int mode = array[0],应该有 int mode = countArray[0] 在循环中,您应该使用 countArray 的大小,而不是数组。 该代码正常工作(结果为 78)

 public static int mode(int[] array){
    int[] countArray = new int[101];

    //counts each number
    for(int i = 0; i < array.length; i++){
      countArray[array[i]]++;
    }// end for loop

    int mode = countArray[0], modeIndex = 0;

    //finds which number occurs the most
    System.out.println(Arrays.toString(countArray));
    for(int i = 1; i < countArray.length; i++){
      if(countArray[i] > mode){

        mode = countArray[i];
        modeIndex = i;
        System.out.println(mode + " " + modeIndex);
      }// end if
    }// end for loop

    return modeIndex;
}// end method mode`

【讨论】:

    【解决方案3】:

    另一种方法是使用 HashMap

    代码:

         int arraya[] = {22, 26, 45, 46, 49, 55, 57, 63, 63, 65, 66, 67, 67, 68, 68, 69, 70, 71, 72, 73, 75, 76, 76, 77, 77, 78, 78, 78, 79, 82, 82, 83, 84, 85, 87, 88, 88, 89, 89, 91, 92, 98, 99};
        List<Integer> array = new ArrayList<>();
        for (int i = 0; i < arraya.length; i++) {
            array.add(arraya[i]);
        }
    
        HashMap<Integer, Integer> count = new HashMap<>();
    
        for (Integer i : array) {
            if (count.containsKey(i)) {
                int c = count.get(i);
                count.put(i, c + 1);
            } else {
                count.put(i, 1);
            }
        }
    
        List listOfOccurance = new ArrayList();
        Set<Map.Entry<Integer, Integer>> entrySet = count.entrySet();
        for (Map.Entry<Integer, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + " = " + entry.getValue());
            listOfOccurance.add(entry.getValue());
    
        }
        Integer i = (Integer) Collections.max(listOfOccurance);
    
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            if ((Integer) pairs.getValue() == i) {
                System.out.println(pairs.getKey());
                return;
            }
            it.remove(); // avoids a ConcurrentModificationException
        }
    

    输出:

    65 = 1 66 = 1 67 = 2 68 = 2 69 = 1 70 = 1 71 = 1 72 = 1 73 = 1 75 = 1 76 = 2 77 = 2 78 = 3 79 = 1 82 = 2 83 = 1 84 = 1 85 = 1 22 = 1 87 = 1 88 = 2 89 = 2 26 = 1 91 = 1 92 = 1 98 = 1 99 = 1 45 = 1 46 = 1 49 = 1 55 = 1 57 = 1 63 = 2 
    The mode is 78
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 2015-09-01
      相关资源
      最近更新 更多