如何计算列表中的唯一项?
人们将如何继续计算列表中唯一项目的数量?
例如,假设我有 {1, 3, 3, 4, 1, 3},我想获取数字 3,它代表列表中唯一项目的数量(即 |A|=3,如果 A={1, 3 , 4})。有人会为此使用什么算法?
我尝试过双循环:
for firstItem to lastItem
currentItem=a
for currentItem to lastItem
currentItem=b
if a==b then numberOfDublicates++
uniqueItems=numberOfItems-numberOfDublicates
这不起作用,因为它计算重复项的次数比实际需要的次数多。对于开头的示例,它将是:
- 对于第一个循环,它将对列表中的数字 1 计算 +1 个重复项。
- 对于第二个循环,它将计算列表中数字 3 的 +2 个重复项。
- 对于第三个循环,它将再次计算数字 3 的+1 个重复项(超过最后一个“3”)并且 这就是问题所在。
知道如何解决这个问题吗?
How would someone go on counting the number of unique items in a list?
For example say I have {1, 3, 3, 4, 1, 3} and I want to get the number 3 which represent the number of unique items in the list(namely |A|=3 if A={1, 3, 4}). What algorithm would someone use for this?
I have tryied a double loop:
for firstItem to lastItem
currentItem=a
for currentItem to lastItem
currentItem=b
if a==b then numberOfDublicates++
uniqueItems=numberOfItems-numberOfDublicates
That doesn't work as it counts the duplicates more times than actually needed. With the example in the beginning it would be:
- For the first loop it would count +1 duplicates for number 1 in the list.
- For the second loop it would count +2 duplicates for number 3 in the list.
- For the third loop it would count +1 duplicates for number 3 again(overcounting the last '3') and
there's where the problem comes in.
Any idea on how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将项目添加到 HashSet,然后在完成后检查 HashSet 的大小。
假设您有一个好的哈希函数,则时间复杂度为
O(n)
。Add the items to a HashSet, then check the HashSet's size after you finish.
Assuming that you have a good hash function, this is
O(n)
.您可以检查该号码后面是否有重复的号码。如果不增加 uniqueCount:
上述方法在时间上是
O(N^2)
,在空间上是O(1)
。另一种方法是对输入数组进行排序,将重复的元素放在一起,然后查找相邻的数组元素。这种方法在时间上是
O(NlgN)
,在空间上是O(1)
。如果允许您使用额外的空间,您可以通过使用哈希在
O(N)
时间和O(N)
空间内完成此操作。哈希的键是数组元素,值是它们的频率。在散列结束时,您只能获得值为
1
的散列键的计数。You can check to see if there are any duplicates following the number. If not increment the uniqueCount:
The above approach is
O(N^2)
in time andO(1)
in space.Another approach would be to sort the input array which will put duplicate elements next to each other and then look for adjacent array elements. This approach is
O(NlgN)
in time andO(1)
in space.If you are allowed to use additional space you can get this done in
O(N)
time andO(N)
space by using a hash. The keys for the hash are the array elements and the values are their frequencies.At the end of hashing you can get the count of only those hash keys which have value of
1
.使用像归并排序或堆排序这样的不错的排序算法对其进行排序(最坏情况都是 O(n log n))并循环排序列表:
Sort it using a decent sorting algorithm like mergesort or heapsort (both habe O(n log n) as worst-case) and loop over the sorted list:
保留字典并在循环中添加计数
这就是 C# 的样子
当然 C# 中有 LINQ 方式,但据我了解,问题很普遍;)
Keep Dictionary and add count in loop
This is how it will look at c#
Of course there is LINQ way in C#, but as I understand question is general ;)