寻找整数的模式数组 [英] Finding the Mode of Integers in an Array

查看:126
本文介绍了寻找整数的模式数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关这个问题,我写的方法称为模式返回整数数组中最频繁出现的元素。假设该阵列具有至少一个元件和所述阵列中的每个元件具有介于0和100(含)之间的值。通过选择较低的值断绝关系。

例如,如果传递的数组包含值{27,15,15,11,27},你的方法应该返回15.(提示:你不妨看看帐簿程序从本章中获得一个想法如何解决这个问题。)

我有看到什么错误了特定的输入问​​题。例如:

模式({27,15,15,27,11,11,11,14,15,15,16,19,99,100,0,27})返回15,它是正确的,但模式({1 ,1,2,3,3})返回3时,它应为1

下面是code:

 公共静态INT模式(INT []输入){
    INT returnVal =输入[0]; //存储元件被返回
    INT的repeatCount = 0; //重复计数的记录数
    INT prevRepCnt = 0; //为重复计数临时    的for(int i = 0; I< input.length;我++){//经过各ELEM        对于(INT J =; J< input.length; J ++){//比较各ELEM第一ELEM后            如果(I = J&安培;!&安培;输入[I] ==输入[J]){//如果匹配值
                的repeatCount ++; //得到重复计数                如果(的repeatCount> = prevRepCnt){//重复比以前更高的计数
                    returnVal =输入[I] //返回该元素
                }
                prevRepCnt =的repeatCount; //保持的最高记录重复
            }
            的repeatCount = 0; //重新设置重复计数明年对比
        }
    }
    返回returnVal;
}


解决方案

下面是解决这个问题的一个简单的方法。创建一个名为尺寸101的数,您指望的指数(0-100)再present数字数组。遍历输入数组并计算每个数字的出现。最后,比较计数找到出现的最(并列进到较小的数字)的一种:

 公共静态INT模式(INT []输入){    INT [] =计数INT新[101];    //算上事件
    的for(int i = 0; I< input.length;我++){
        算上[输入[I]] ++;
    }    //往回走,找到最出现计数
    INT指数= count.length-1;
    的for(int i = count.length-2; I> = 0;我 - ){
        如果(计数[I]> =计[指数])
            指数= I;
    }    返回指数;
}

For this problem, I am to write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value.

For example, if the array passed contains the values {27, 15, 15, 11, 27}, your method should return 15. (Hint: You may wish to look at the Tally program from earlier in this chapter to get an idea of how to solve this problem.)

I am having a problem seeing what is going wrong for a specific input. For instance:

mode({27, 15, 15, 27, 11, 11, 11, 14, 15, 15, 16, 19, 99, 100, 0, 27}) returns 15 which is correct, but mode({1, 1, 2, 3, 3}) returns 3 when it should be 1.

Here is the code:

public static int mode(int[] input) {
    int returnVal = input[0]; // stores element to be returned
    int repeatCount = 0; // counts the record number of repeats
    int prevRepCnt = 0; // temporary count for repeats

    for (int i=0; i<input.length; i++) { // goes through each elem

        for (int j=i; j<input.length; j++) { // compares to each elem after the first elem

            if (i != j && input[i] == input[j]) { // if matching values
                repeatCount++; // gets the repeat count

                if (repeatCount>=prevRepCnt) { // a higher count of repeats than before
                    returnVal=input[i]; // return that element
                }
                prevRepCnt = repeatCount; // Keeps the highest repeat record
            }
            repeatCount=0; // resets repeat Count for next comparison
        }
    }
    return returnVal;
}

解决方案

Here's a simpler way to solve this problem. Create an array called count of size 101. The indexes (0-100) represent the numbers you are counting. Traverse the input array and count the occurrences of each number. Finally, compare the counts to find the one that appears the most (tie goes to the lower number):

public static int mode(int[] input) {

    int[] count = new int[101];

    //count the occurrences
    for (int i=0; i < input.length; i++) {
        count[input[i]]++;
    }

    //go backwards and find the count with the most occurrences
    int index = count.length-1;
    for (int i=count.length-2; i >=0; i--) {
        if (count[i] >= count[index])
            index = i;
    }

    return index;
}

这篇关于寻找整数的模式数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆