计算数组中数字的频率
我有一个数组 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我只需创建一个
HashMap
,其中第一个整数是分数数组中的值,第二个整数是频率。然后处理数组填充到hashmap中。如果某个键已经存在,则将计数加一。如果是新密钥,请将其设置为 1。
然后处理哈希图以找到出现次数最多的值。
一旦我能够访问安装了 Java 的机器,我就打算研究源代码,但是,由于它现在被标记为家庭作业,所以它只是算法(从长远来看,这对你来说会更好):
这是我的基本算法接下来将由两个顺序循环组成。
第一个只是创建值到计数的映射,以便您稍后可以找到最大计数。
第二个遍历映射并创建具有最高计数的值的列表。
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):
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.
嗯,有两部分:迭代分数并存储每个分数的频率。它们都涉及使用数组/数组列表。如果您需要更多帮助,我们可以直接提问。 :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
由于分数可能在有界范围内(例如
0..100
),因此您可以使用计数数组来快速完成此操作。基本上,您正在执行计数排序的第一阶段。所有可能分数的
count
从0
开始,然后对于每个分数s
,递增count[s]
。处理完所有分数后,扫描count
并查看哪个count[k]
最高。您还可以在计数时跟踪最常见的分数。只需执行以下操作:
由于您的分数排列在二维矩阵中(出于某种原因?),您可以按如下方式处理每个分数:
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 at0
, then for each scores
, incrementcount[s]
. Once all scores are processed, scan thecount
and see whichcount[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:
Since your scores are arranged in a 2d matrix (for some reason?), you can process each score as follows:
或者随时保留指向当前最大的指针。每次创建或更新时,请比较一下是否超出了之前的最大值,如果超出则替换它。
保存另一遍哈希图。
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.
对于这样的事情,编写代码来模拟您在现实生活中的做事方式。
让我们建模:
您的 [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.