Finding the Mode of Integers in an Array

橙三吉。 提交于 2019-12-05 17:43:01

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;
}

I have recently analyzed four ways to calculate mode of the array:

  • If range of numbers in the array is small then use counting sort - O(N) time, (N) space but very efficient. This solution is directly applicable to the problem you are asking about, since you have only 101 possible values in the array.
  • Index elements in the array in hash table - O(N) time, O(N) space. This solution is applicable if values are taken from a large range, such as all integer numbers.
  • Sort the array and then count successive equal elements - O(NlogN) time, O(1) space. This solution is applicable if array is too large to build an index.
  • Partially sort the array but skip partitions smaller than current candidate - O(NlogN) time, O(1) space but much more efficient than fully sorting the array because many partitions will be skipped.

You can find source code (although in C#) for all four methods and performance comparison in this article: Finding Mode of an Array

I would declare another variable to keep track of the "lower value". And check if the input[i] value is smaller than the lowerValue variable when it has the same count. Note I separated the > & = for your condition.

int lowerValue;

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
    int lowerValue = Integer.MAX_VALUE; // initalize it with the highest integer value - 2147483647

    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
                    lowerValue = returnVal; // set the variable lowerValue to be the lower value
                }
                else if (repeatCount == prevRepCnt) && (input[i] < lowerValue) { // if it's the same number of count, take in whichever number is lower
                    returnVal=input[i]; // return that element
                    lowerValue = returnVal; // set the variable lowerValue to be the lower value
                }
                prevRepCnt = repeatCount; // Keeps the highest repeat record
            }
            repeatCount=0; // resets repeat Count for next comparison
        }
    }
    return returnVal;
}

Base on your code all you need to change is.

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 is what you need to change.

if (repeatCount>prevRepCnt) { // a higher count of repeats than before
  • take out the equal sign and you should be good.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!