如何在数组列表中查找相同整数的倍数?
我的问题如下。我有一个整数数组列表。 arraylist 包含 5 个整数,例如 [5,5,3,3,9] 或可能 [2,2,2,2,7]。许多数组列表都有重复的值,我不确定如何计算每个值存在多少个。
问题是如何在数组列表中找到重复值并计算该特定重复值的数量。在第一个示例 [5,5,3,3,9] 中,有 2 个 5 和 2 个 3。 [2,2,2,2,7] 的第二个例子只有 4 个 2。我希望找到的结果信息是是否存在重复项,其中有多少个以及重复的特定整数。
我不太确定如何在 java 中执行此操作。
任何帮助将不胜感激。谢谢。
My problem is as follows. I have an arraylist of integers. The arraylist contains 5 ints e.g[5,5,3,3,9] or perhaps [2,2,2,2,7]. Many of the arraylists have duplicate values and i'm unsure how to count how many of each of the values exist.
The problem is how to find the duplicate values in the arraylist and count how many of that particular duplicate there are. In the first example [5,5,3,3,9] there are 2 5's and 2 3's. The second example of [2,2,2,2,7] would be only 4 2's. The resulting information i wish to find is if there are any duplicates how many of them there are and what specific integer has been duplicated.
I'm not too sure how to do this in java.
Any help would be much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说,最直接的答案是使用
Collections.Frequency
方法。大致如下:如果您愿意,您可以将每个
Integer
与其出现次数进行映射:To me, the most straightforward answer, would be using the
Collections.frequency
method. Something along the lines of this:If you want to, you could map each
Integer
with its number of occurrences:我想到了两种算法。
对它进行排序(
Collections.sort
)。然后通过迭代轻松找到欺骗者。通过在
Map
中保存计数(或Map
对于可变计数)进行迭代。这个样子有点丑啊不管怎样,编码应该是一个有启发性的练习。我建议两者都做,然后进行比较。
Two algorithms spring to mind.
Sort it (
Collections.sort
). Then iterate through easily finding dupes.Iterate through keeping count in a
Map<Integer,Integer>
(orMap<Integer,AtomicInteger>
for a mutable count). A bit ugly this way.Either way, coding it should be an instructive exercise. I suggest doing both, and comparing.
这是我在 @Tom 的答案的评论中描述的具体实现,经过测试:
Here is a concrete implementation, with test, of what I described in comments to @Tom's answer:
除了数组列表之外,还使用 Hashmap 集合,其中
遍历数组列表,将这些值收集到哈希图中,当先前的键不存在时添加一个新项目,并将已存在的键的值加 1。然后迭代 Hashmap 并打印出值 > 的所有键。 1.
Use a Hashmap collection in addition to the array list where
Walk your array list collecting these values into the hashmap adding a new item when a previous key does not exist and incrementing by 1 the values of keys that do already exist. Then iterate over the Hashmap and print out any keys where the value is > 1.
您可以浏览
List
并将它们放入带有计数的Map
中。然后很容易找出哪一个是重复的。You can go through the
List
and put them in aMap
with the count. Then it is easy figure out which one is duplicated.为了更清晰地抽象您正在做的事情,您可以使用 Multiset 数据结构,来自 guava/ Google 集合。您甚至可能会发现您宁愿使用它而不是
List
,具体取决于您使用它做什么(如果您不需要列表的确定性排序)。您可以这样使用它:就上面的幕后操作而言,它几乎完全等同于基于您的列表构建
Map
的建议。For a cleaner abstraction of what you're doing, you could use the Multiset data structure from guava/google-collections. You may even find you'd rather use it than a
List
, depending on what you're doing with it (if you don't need the deterministic ordering of a list). You'd use it like this:In terms of what the above is doing under the covers, it's almost exactly equivalent to the suggestion of building a
Map<Integer,AtomicInteger>
based on your list.