如何计算列表中的唯一项?

发布于 2024-10-22 00:31:51 字数 611 浏览 5 评论 0原文

人们将如何继续计算列表中唯一项目的数量?

例如,假设我有 {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 计算 +1 个重复项。
  2. 对于第二个循环,它将计算列表中数字 3 的 +2 个重复项。
  3. 对于第三个循环,它将再次计算数字 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:

  1. For the first loop it would count +1 duplicates for number 1 in the list.
  2. For the second loop it would count +2 duplicates for number 3 in the list.
  3. 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 技术交流群。

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

发布评论

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

评论(5

水晶透心 2024-10-29 00:31:51

将项目添加到 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).

云巢 2024-10-29 00:31:51

您可以检查该号码后面是否有重复的号码。如果不增加 uniqueCount:

uniqueCount = 0;
for (i=0;i<size;i++) {
  bool isUnique = true;
  for (j=i+1;j<size;j++)
     if (arr[i] == arr[j] {
       isUnique = false;
       break;
     }
  }
  if(isUnique) {
    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:

uniqueCount = 0;
for (i=0;i<size;i++) {
  bool isUnique = true;
  for (j=i+1;j<size;j++)
     if (arr[i] == arr[j] {
       isUnique = false;
       break;
     }
  }
  if(isUnique) {
    uniqueCount ++;
  }
}

The above approach is O(N^2) in time and O(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 and O(1) in space.

If you are allowed to use additional space you can get this done in O(N) time and O(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.

无法回应 2024-10-29 00:31:51

使用像归并排序或堆排序这样的不错的排序算法对其进行排序(最坏情况都是 O(n log n))并循环排序列表:

sorted_list = sort(list)
unique_count = 0
last = sorted_list[0]

for item in sorted_list[1:]:
  if not item == last:
    unique_count += 1
  last = item

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:

sorted_list = sort(list)
unique_count = 0
last = sorted_list[0]

for item in sorted_list[1:]:
  if not item == last:
    unique_count += 1
  last = item
囍孤女 2024-10-29 00:31:51
list.sort();
for (i = 0; i < list.size() - 1; i++)
  if (list.get(i)==list.get(i+1)
    duplicates++;
list.sort();
for (i = 0; i < list.size() - 1; i++)
  if (list.get(i)==list.get(i+1)
    duplicates++;
贵在坚持 2024-10-29 00:31:51

保留字典并在循环中添加计数

这就是 C# 的样子

int[] items = {1, 3, 3, 4, 1, 3};
Dictionary<int,int> dic = new Dictionary<int,int>();
foreach(int item in items)
   dic[item]++

当然 C# 中有 LINQ 方式,但据我了解,问题很普遍;)

Keep Dictionary and add count in loop

This is how it will look at c#

int[] items = {1, 3, 3, 4, 1, 3};
Dictionary<int,int> dic = new Dictionary<int,int>();
foreach(int item in items)
   dic[item]++

Of course there is LINQ way in C#, but as I understand question is general ;)

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