简体   繁体   English

你如何在java中找到数组的模式?

[英]How do you find the mode of an Array in java?

I'm new to java and I have a homework assignment where I need to find the Mean, median and Mode of an Array.我是 Java 新手,我有一个家庭作业,我需要在其中找到数组的均值、中值和众数。 For some reason my code is not putting out the correct answer.出于某种原因,我的代码没有给出正确的答案。

Here is the code I was provided to create the Arrays:这是我提供的用于创建数组的代码:

public static void main(String[] args) {

    int[] test01 = new int[]{2,2,2,2,2};
    int[] test01Results = new int[]{2,2,2,2};

    int[] test02 = new int[]{1,2,1,3,5,6,6,1,2,2,2,99,100};
    int[] test02Results = new int[]{2,2,17,100};

    int[] test03 = new int[]{100,200,300,400,300};
    int[] test03Results = new int[]{300,300,260,400};

    int[] test04 = new int[]{100};
    int[] test04Results = new int[]{100,100,100,100};

    int[] test05 = new int[]{100,1};
    int[] test05Results = new int[]{1,100,50,100};

Here is what I came up with to try to calculate the Mode:这是我想出的尝试计算模式的方法:

public int mode() {
    int result = 0;
    // Add your code here

    int repeatAmount = 0; //the amount of repeats accumulating for the current i
    int highestRepeat=0; // the highest number of repeats so far
        for (int i=0; i<numberArray.length; i++) { 
            for (int j=i; j<numberArray.length; j++) { 
                if (i != j && numberArray[i] == numberArray[j]) { 
                    repeatAmount++;

                    if (repeatAmount>highestRepeat) { 
                        result=numberArray[i];
                    }
                    repeatAmount = highestRepeat; 
                }
                repeatAmount=0; // resets repeat Count for next comparison
            }
        }
    return result;
}

I'm getting the correct results for tests 1, 2 and 3 but getting the wrong result for Tests 4 and 5. Does anyone know what I'm doing wrong?我在测试 1、2 和 3 中得到了正确的结果,但在测试 4 和 5 中得到了错误的结果。有谁知道我做错了什么?

Thanks!谢谢!

You never assign anything except 0 to highestRepeat .除了 0 之外,您永远不会将任何内容分配给highestRepeat This should work:这应该有效:

public int mode() {
    int result = 0;
    int highestRepeat=0;
    for (int i=0; i<numberArray.length; i++) { 
        int repeatAmount = 1;
        for (int j = i + 1; j < numberArray.length; j++) { 
            if (numberArray[i] == numberArray[j]) { 
                repeatAmount++;
                if (repeatAmount > highestRepeat) { 
                    result = numberArray[i];
                    highestRepeat = repeatAmount;
                }
            }
        }
    }
    return result;
}

Some other improvements:其他一些改进:

  • By starting the inner loop at i+1 you can skip the check if i != j .通过在i+1处启动内部循环,您可以跳过检查i != j
  • By declaring repeatAmount inside the outer loop you can skip setting it to zero after the inner loop.通过在外循环内声明repeatAmount ,您可以在内循环后跳过将其设置为零。

If you need some performance, consider using a HashMap for counting the equal array entries.如果您需要一些性能,请考虑使用HashMap来计算相等的数组条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM