如何编写代码来查找数组中整数的模式而不使用个人创建的方法,即只是常规 "raw code"

How to write a code to find the mode of the ints in the array without using a personally created method ie just regular "raw code"

我正在编写代码来查找随机生成的整数数组的均值、中位数和众数(用户输入数组的大小和要生成随机数的范围,它会生成介于 3 之间的数字-22 随机。我在为平均值或中位数编写代码时没有遇到太多麻烦,但我似乎无法编写代码来计算模式(最常出现的数字)。任何人都可以帮忙或 show/put 代码如何计算随机生成的整数数组的模式,而不必在代码中为自己创建一个方法?谢谢。这是我目前所拥有的(找到均值和中位数的代码):

    public class ArraysIntsMeanMedianAndMode {
public static void main(String[] args) {
    int ArraySize;
    int min;
    int max;
    double x;
    // To get the Size and range of numbers for the randomly genereated ints in the array.
    Scanner sc = new Scanner(System.in);
    System.out.println("What size should the array be?");
    ArraySize = sc.nextInt();
    System.out.println("Please enter a minimum value for the range of ints.");
    min = sc.nextInt();
    System.out.println("Please enter a maximum value for the range of ints.");
    max = sc.nextInt();
    //Making the array and filling it based on the user inputs
    int[] MMMarray = new int[ArraySize];
    int total = 0;
    for (int i = 0; i < ArraySize; i++) {
        x = (int) (Math.random() * ((max - min) + 1)) + min;
        System.out.println(x);
        int RandoNums = (int) x;
        total = total + RandoNums;
        MMMarray[i] = RandoNums;
    }
    //Finding mean/average
    double Mean = (total + 0.0) / ArraySize;
    System.out.println("The mean is: " + Mean);
    //Finding Median/Middle number
    Arrays.sort(MMMarray);
    System.out.println(Arrays.toString(MMMarray));
    if (ArraySize % 2 == 0) {
        System.out.println("The median is: " + ((MMMarray[(ArraySize / 2)] + 0.0) + MMMarray[(ArraySize / 2) - 1]) / 2 + ".");
    } else System.out.println("The median is: " + MMMarray[ArraySize / 2] + ".");
    //How to find mode???????? 

未排序int数组的查找方式:

int freq = 0;
int value = 0;
int length = MMMArray.length;

for (int outer = 0; outer < length; outer++)
{
    int tempFreq = 0;

    for (int inner = 0; inner < length; inner++)
    {
        if (MMMArray[outer] == MMMArray[inner])
        {
            tempFreq++;
        }
    }

    if (tempFreq > freq)
    {
        freq = tempFreq;
        value = MMMArray[outer];
    }
}

System.out.println("Mode is " + value + ", which appears " + freq + " times.");

因为您已经对数组进行了排序以计算中位数,所以找到众数的问题就等同于找到相同数字的最长连续条纹。因此,例如,如果您有 [1, 1, 2, 2, 2, 3, 5, 5, 21],则有三个连续的 2,比任何其他 运行 都长,因此 2 是模式。

要找到最长的运行,您可以再次传递数据,而不是读取任何元素两次。我正在稍微调整 Litvin and Litvin 的代码以使用您的数组名称,将 1 的 运行 计为 运行,并报告模式的数字而不是位置它在数组中。在计算中位数后,您可以将此代码放在您提出问题的地方。

 // at this point MMMArray is a sorted, nonempty array of int, because it was already sorted to find the median
int maxRunStart = 0, maxRunLength = 1;
int runStart = 0, runLength = 1;

for (int i = 1; i <= MMMArray.length; i++) //what they do here by using <= 

//rather than < is worth reflecting upon
//it handles the case of the biggest run being at the end within the loop body
    {
      if (i < MMMArray.length && MMMArray[i] == MMMArray[i - 1])//notice how the boolean short-circuit prevents reading beyond the end of the array
      {
        runLength++;
      }
      else
      {
        if (runLength > maxRunLength)
        {
          maxRunStart = runStart;
          maxRunLength = runLength;
        }
        runStart = i;
        runLength = 1;
      }
    }
      System.out.println("The mode is: " + MMMArray[maxRunStart] + ".");
    }

现在有一些新的东西需要思考。假设 MMMArray 包含 [1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3]。此代码(或 MarsAtomic 的代码)将报告 1 是唯一模式。但是数据是双峰的,3 和 1 一样多。调整代码的一种方法是将模式存储在数组列表(或数组,因为我们预先知道模式不能比数字更多)。我认为再传递一次数据更简单(不是更高效,只是更容易不搞砸并且不引入另一种非简单类型)。如果你想要那样,那么在第一个 for 循环之后,而不是 one 模式的 println,插入以下内容:

runLength = 1;
runStart = 0;
for (int i = 1; i <= MMMArray.length; i++)
{
  if (i < MMMArray.length && MMMArray[i] == MMMArray[i - 1])
  {
    runLength++;
  }
  else
  {
    if (runLength == maxRunLength)
    {
        System.out.println("The mode is: " + MMMArray[runStart] + ".");
    }
    runStart = i;
    runLength = 1;
  }
}