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

时间:2022-03-24 21:32:02

I'm new to java and I have a homework assignment where I need to find the Mean, median and Mode of an Array. For some reason my code is not putting out the correct answer.

我是java新手,我有一个作业,我需要找到数组的平均值,中位数和模式。由于某种原因,我的代码没有给出正确的答案。

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!

谢谢!

1 个解决方案

#1


0  

You never assign anything except 0 to highestRepeat. This should work:

除了0到highestRepeat之外,您从不分配任何东西。这应该工作:

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的内部循环,您可以跳过检查if i != j。
  • By declaring repeatAmount inside the outer loop you can skip setting it to zero after the inner loop.
  • 通过在外部循环中声明重复量,可以跳过内部循环之后将其设置为零。

If you need some performance, consider using a HashMap for counting the equal array entries.

如果您需要一些性能,可以考虑使用HashMap来计数相同的数组条目。

#1


0  

You never assign anything except 0 to highestRepeat. This should work:

除了0到highestRepeat之外,您从不分配任何东西。这应该工作:

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的内部循环,您可以跳过检查if i != j。
  • By declaring repeatAmount inside the outer loop you can skip setting it to zero after the inner loop.
  • 通过在外部循环中声明重复量,可以跳过内部循环之后将其设置为零。

If you need some performance, consider using a HashMap for counting the equal array entries.

如果您需要一些性能,可以考虑使用HashMap来计数相同的数组条目。