计算数组中数字的频率

发布于 2024-08-30 19:53:24 字数 76 浏览 2 评论 0原文

我有一个数组 scores[5][5] 它充满了测试分数。

我需要找到最常出现的分数并将其返回。

I have an array, scores[5][5] and it is filled with test scores.

I need to find the most frequently occurring score and return it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

躲猫猫 2024-09-06 19:53:24

我只需创建一个 HashMap ,其中第一个整数是分数数组中的值,第二个整数是频率。

然后处理数组填充到hashmap中。如果某个键已经存在,则将计数加一。如果是新密钥,请将其设置为 1。

然后处理哈希图以找到出现次数最多的值。


一旦我能够访问安装了 Java 的机器,我就打算研究源代码,但是,由于它现在被标记为家庭作业,所以它只是算法(从长远来看,这对你来说会更好):

Create empty hashmap freq
For each entry in your array (probably nested loops):
    If entry.value is not in freq:
        Add entry.value to freq, set its count to zero
    Increase count of freq.value
Set max_count to 0
For each item in freq:
    If item.count is greater than max_count:
        Set max_list to an empty list
        Set max_count to item.count
    If item.count is equal to max_count:
        Append item.value to max_list
If max_count is greater than 0:
    Output max_count, max_list

这是我的基本算法接下来将由两个顺序循环组成。

第一个只是创建值到计数的映射,以便您稍后可以找到最大计数。

第二个遍历映射并创建具有最高计数的值的列表。

I would simply create a HashMap<Integer,Integer> where the first integer is the value in the scores array and the second is the frequency.

Then process the array filling in the hashmap. If a key already existed, up the count by one. If it's a new key, set it to one.

Then process the hashmap to find the value with the largest occurrence.


I was going to work on the source code once I got access to a machine where Java was installed but, since it's now marked homework, it's algorithms only (which will be better for you in the long run anyway):

Create empty hashmap freq
For each entry in your array (probably nested loops):
    If entry.value is not in freq:
        Add entry.value to freq, set its count to zero
    Increase count of freq.value
Set max_count to 0
For each item in freq:
    If item.count is greater than max_count:
        Set max_list to an empty list
        Set max_count to item.count
    If item.count is equal to max_count:
        Append item.value to max_list
If max_count is greater than 0:
    Output max_count, max_list

That's the basic algorithm I would follow which consists of two sequential loops.

The first simply creates a mapping of values to counts so that you can find the greatest count later.

The second runs through the map and creates a list of the values with the highest count.

混浊又暗下来 2024-09-06 19:53:24

嗯,有两部分:迭代分数并存储每个分数的频率。它们都涉及使用数组/数组列表。如果您需要更多帮助,我们可以直接提问。 :D

Well, there's 2 parts: iterating the scores and storing the frequency of each score. Both of them involve using an array/arraylist. We can direct questions if you want more help. :D

云醉月微眠 2024-09-06 19:53:24

由于分数可能在有界范围内(例如 0..100),因此您可以使用计数数组来快速完成此操作。基本上,您正在执行计数排序的第一阶段。

所有可能分数的 count0 开始,然后对于每个分数 s,递增 count[s]。处理完所有分数后,扫描 count 并查看哪个 count[k] 最高。

您还可以在计数时跟踪最常见的分数。只需执行以下操作:

// init
mostFrequent = 0;

// during increment
count[s]++;
if (count[s] > count[mostFrequent]) {
  mostFrequent = s;
}

由于您的分数排列在二维矩阵中(出于某种原因?),您可以按如下方式处理每个分数:

int[] count = new int[RANGE]; // valid scores are 0..RANGE-1

mostFrequent = 0;
for (int[] tuplet : scores) {
   for (int s : tuplet) {
     count[s]++;
     if (count[s] > count[mostFrequent]) {
        mostFrequent = s;
     }
   }
}

report(mostFrequent, count[mostFrequent]);

Since scores are likely to be in a bounded range (e.g. 0..100), you can use a counting array to do this quickly. Basically, you're doing the first phase of counting sort.

The count for all possible scores starts at 0, then for each score s, increment count[s]. Once all scores are processed, scan the count and see which count[k] is the highest.

You can also keep track of the most frequent score as you're doing the counting. Just do something like this:

// init
mostFrequent = 0;

// during increment
count[s]++;
if (count[s] > count[mostFrequent]) {
  mostFrequent = s;
}

Since your scores are arranged in a 2d matrix (for some reason?), you can process each score as follows:

int[] count = new int[RANGE]; // valid scores are 0..RANGE-1

mostFrequent = 0;
for (int[] tuplet : scores) {
   for (int s : tuplet) {
     count[s]++;
     if (count[s] > count[mostFrequent]) {
        mostFrequent = s;
     }
   }
}

report(mostFrequent, count[mostFrequent]);
南城旧梦 2024-09-06 19:53:24

或者随时保留指向当前最大的指针。每次创建或更新时,请比较一下是否超出了之前的最大值,如果超出则替换它。
保存另一遍哈希图。

Or keep a pointer to the the current largest one as you go. Each time you create or update, compare to see if you've just exceeded the previous largest and replace it if you have.
Saves another pass through the hashmap.

反话 2024-09-06 19:53:24

对于这样的事情,编写代码来模拟您在现实生活中的做事方式。

让我们建模:

您的 [5][5] 数组:它只是一个 5 列 5 行的数字网格。

从 0,0 位置开始 - 读取该位置的值,并启动一个列表(在 Java 中为 ArrayList 或 HashMap),将数字添加到列表中并给它一个哈希标记(值为 1),以指示您见过一次。

穿过这一行,然后向左返回并向下一行,等等。

您阅读的每个号码,请检查它是否已在您的列表中。如果是,只需再做一个哈希标记(加 1)。如果没有,则将该号码添加到您的列表中并为其添加哈希标记。

读完数组后,从头开始查看列表,并通过将手指放在它上面来跟踪您看到的具有最多哈希标记的数字(将数字存储在变量中)。

返回最后一个变量。

For things like this, write code that models how you would do things in real-life.

Let's model:

Your [5][5] array: It's just a grid of numbers with 5 columns and 5 rows.

Start at the 0,0 position - read the value at that position, and start a list (in Java, an ArrayList or HashMap), add the number to the list and give it a hash mark (value of 1), to indicate you've seen it once.

Go across the row and then back left and down a row, et al.

Each number you read, check if it's already on your list. If it is, just make another hash mark (add 1). If not, then add the number to your list and give it a hash mark.

After you finish reading the array, look at your list, from the beginning, and keep track of the number with the most hash marks you've seen by putting your finger on it (storing the number in a variable).

Return that last variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文