简体   繁体   English

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

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

Write a full Java program that does the following: 编写一个完整的Java程序,该程序执行以下操作:

  • Creates an array of 100 double. 创建一个由100个double组成的数组。

  • Reads in an unknown number of doubles from a file named values.txt . 从名为values.txt的文件中读取未知数量的double。

  • There will be at least 2 distinct values, and no more than 100 distinct values in the file. 文件中将至少有2个不同的值,并且不超过100个不同的值。 The values will be in unsorted order. 这些值将按未排序的顺序排列。 Values will be no smaller than 0, and no larger than 99. 值将不小于0,且不大于99。

  • Outputs the most frequently occurring value in the file. 输出文件中最频繁出现的值。

  • Outputs the least frequently occurring value in the file. 输出文件中出现频率最低的值。 The value must occur at least once in order to be output. 该值必须至少出现一次才能输出。

  • Outputs the average of all array values. 输出所有数组值的平均值。

  • You must create and use separate methods for each of the items #2-5. 您必须为#2-5中的每一项创建并使用单独的方法。

This is what I have so far. 到目前为止,这就是我所拥有的。 I cannot for the life of me figure out how to get this right: 我一生无法解决如何正确解决这个问题:

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

Notice I am required to make an array of double even though it is not necessary. 请注意,即使没有必要,我也需要制作一个double数组。

If all values are int than you should use int array instead of double . 如果所有值都是int ,则应使用int数组而不是double As all values in range 0-99. 所有值都在0-99范围内。 So, you can increase input value frequency. 因此,您可以增加输入值的频率。 Look at below logic: 看下面的逻辑:

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

Now calculate the maximum frequency from freqArr 现在从freqArr计算最大频率

 int maxFreq=0;
 for(int freq : freqArr){
   if(maxFreq < freq){
      maxFreq = freq;
   }
 }

Note: If double array is mandatory than you can also use double array like: 注意:如果必须使用double array,则还可以使用double array,例如:

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

It's possible to find a most-occurring value without sorting like this: 无需这样排序就可以找到最常出现的值:

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

Pham Thung is right : - You read in integer inFile.nextInt(), why do you need to use double array to store them? Pham Thung是正确的:-您读入infile.nextInt()整数,为什么需要使用双精度数组来存储它们? – Pham Thung –范通

You can achieve your first functionality in n time if its integer array. 如果它是整数数组,则可以在n天内实现您的第一个功能。

you question says, 您的问题是,

Values will be no smaller than 0, and no larger than 99. 值将不小于0,且不大于99。

So, 1. Make an array of size 100.(Counter[]) 2. Iterate through values of your current array and add count to Counter array. 因此,1.制作一个大小为100.(Counter [])的数组。2.遍历当前数组的值,然后将计数添加到Counter数组。

eg: if double array contains 2 3 2 5 0 0 0 例如:如果double数组包含2 3 2 5 0 0 0

Our counter array will be like 我们的计数器阵列将像
location : 0 1 2 3 4 5 6 ...........100 位置:0 1 2 3 4 5 6 ...... 100

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

and so on. 等等。

You can use below algorithm for this 您可以为此使用以下算法

  1. Sort the array (you only need to read unsorted array, but you can sort the array once read from the file) 对数组进行排序(您只需要读取未排序的数组,但是一旦从文件中读取就可以对数组进行排序)
  2. Make double var : num, mostCommon, count = 0, currentCount = 1 设为double var:num,mostCommon,count = 0,currentCount = 1
  3. Assign Arr1[0] to num 将Arr1 [0]分配给num
  4. for i from 1 to length of Arr1 对于我从1到Arr1的长度

    i. 一世。 if(Arr1[i] == num) if(Arr1 [i] == num)

    a. 一种。 Increment currentCount 增加currentCount

    ii. ii。 else 其他

    a. 一种。 if(count > currentCount) if(count> currentCount)

    A. Assign currentCount to count A.分配currentCount进行计数

    B. Assign num to mostCommon B.将num分配给mostCommon

    C. Assign Arr1[i] to num C.将Arr1 [i]分配给num

    D. Assign 1 to currentCount D.为currentCount分配1

At the end of this loop, you will have most common number in mostCommon var and it's number of occurrence in count. 在此循环结束时,您将在mostCommon变量中拥有最常见的数字,它是出现的次数。

Note : I don't know how to format the algo 注意:我不知道如何格式化算法

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

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