在 Java 中的 double 数组中找到最频繁的值(没有哈希图或排序)

Find the most frequent value in an array of double in Java (without hashmaps or sorting)

编写一个完整的 Java 程序来执行以下操作:

这就是我目前所拥有的。我这辈子都想不出如何做到这一点:

import java.util.*;
import java.io.*;

public class arrayProgram2 {

    static Scanner console = new Scanner(System.in);
    static final int ARRAY_SIZE = 100;
    static int numOfElements = 0;

    public static void main(String[] args) throws FileNotFoundException {
        Scanner inFile = new Scanner(new FileReader("values.txt"));
        double[] Arr1 = new double[ARRAY_SIZE];

        while (inFile.hasNext()) {
            Arr1[numOfElements] = inFile.nextDouble();
            numOfElements++;
        }

        System.out.println("There are " + numOfElements + " values.");
        System.out.printf("The average of the values is %.2f%n", avgArray(Arr1));
        System.out.println("The sum is " + sumArray(Arr1));

        inFile.close();

    } //end main

    //Method to calculate the sum
    public static double sumArray(double[] list) {
        double sum = 0;

        for (int index = 0; index < numOfElements; index++) {
            sum = sum + list[index];
        }

        return sum;
    }

    //Method to calculate the average
    public static double avgArray(double[] list) {
        double sum = 0;
        double average = 0;

        for (int index = 0; index < numOfElements; index++) {
            sum = sum + list[index];
        }

        average = sum / numOfElements;
        return average;
    }
} //end program

请注意,我需要制作一个 double 数组,尽管这不是必需的。

无需像这样排序就可以找到出现次数最多的值:

static int countOccurrences(double[] list, double targetValue) {
    int count = 0;
    for (int i = 0; i < list.length; i++) {
        if (list[i] == targetValue)
            count++;
    }
}


static double getMostFrequentValue(double[] list) {
    int mostFrequentCount = 0;
    double mostFrequentValue = 0;
    for (int i = 0; i < list.length; i++) {
        double value = list[i];
        int count = countOccurrences(list, value);
        if (count > mostFrequentCount) {
            mostFrequentCount = count;
            mostFrequentValue = value;
        }
    }
    return mostFrequentValue;
}

如果所有值都是 int,那么您应该使用 int 数组而不是 double。作为 0-99 范围内的所有值。因此,您可以增加输入值频率。看下面的逻辑:

 int[] freqArr= new int[100];
 while (inFile.hasNext()){
   int value = inFile.nextInt();
   freqArr[value]++;  // count the frequency of selected value.
 } 

现在从freqArr

计算最大频率
 int maxFreq=0;
 for(int freq : freqArr){
   if(maxFreq < freq){
      maxFreq = freq;
   }
 }

注意: 如果双数组是强制性的,那么您也可以使用双数组,如:

 double[] freqArr= new double[100];
 while (inFile.hasNext()){
   freqArr[(int)inFile.nextDouble()]++;
 }

Pham Thung 是对的:- 你读的是整数inFile.nextInt(),为什么要用double数组来存储呢? – 范童

如果是整数数组,您可以在 n 时间 内实现您的第一个功能。

你的问题是,

值将不小于 0,不大于 99。

所以, 1. 制作一个大小为 100 的数组。(Counter[]) 2. 遍历当前数组的值并将计数添加到计数器数组。

例如: 如果双数组包含 2 3 2 5 0 0 0

我们的计数器数组会像
位置:0 1 2 3 4 5 6 .....................100

值:3 0 1 1 0 1 0 ...............

等等。

您可以为此使用以下算法

  1. 对数组进行排序(只需要读取未排序的数组,但从文件中读取后即可对数组进行排序)
  2. 制作双变量:num, mostCommon, count = 0, currentCount = 1
  3. 将 Arr1[0] 分配给 num
  4. for i 从 1 到 Arr1 的长度

    我。如果(Arr1[i] == num)

    一个。增加 currentCount

    二。其他

    一个。如果(计数 > 当前计数)

    一个。将 currentCount 分配给 count

    乙。将 num 分配给 mostCommon

    C。将 Arr1[i] 分配给 num

    D.将 1 分配给 currentCount

在此循环结束时,您将在 mostCommon var 中得到最常见的数字,它在 count 中出现的次数。

注意:我不知道如何格式化算法