如何获得最频繁的物品 [英] how to get the most frequent items

查看:89
本文介绍了如何获得最频繁的物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的应用程序具有包含数字行的大型数组,

I am working on an application which has a large array containing lines of numbers,

transNum[20000][200]//this is the 2d array containing the numbers and always keep track of the line numbers

我正在使用嵌套循环以查找最常见的项目。

I am using a nested loop to look for the most frequent items. which is

for(int i=0/*,lineitems=0*/;i<lineCounter;i++)
  {
      for(int j=0,shows=1;j<lineitem1[i];j++)
      {
          for(int t=i+1;t<lineCounter;t++)
          {
              for(int s=0;s<lineitem1[t];s++)
              {
                  if(transNum[i][j]==transNum[t][s])
                      shows++;
              }
          }

          if(shows/lineCounter>=0.2)
          {

              freItem[i][lineitem2[i]]=transNum[i][j];
              lineitem2[i]++;
          }
      }

  }

我正在使用像test [200] [200]这样的小型输入数组进行测试,此循环工作正常并且计算时间可以接受,但是当我尝试处理包含12000行的数组时,计算时间过长,所以我在想如果还有其他方法可以计算频繁项目而不是使用此循环。我仅对10688行进行了测试,获得所有频繁项目的时间为825805ms,这很昂贵。

when I was doing tests using small input arrays like test[200][200], this loop works fine and the computing time is acceptable, but when I try to process the array contains 12000 lines, the computing time is too long, so I am thinking if there are other ways to compute the frequent items rather than using this loop.I just ran a test on 10688 lines, and the time to get all the frequent item is 825805ms, which is way to expensive.

推荐答案

取决于您的输入。如果您还以相同的代码插入数据,则可以在插入频繁项目时对其进行计数。

Depends on your input. If you are also inserting the data in the same code then you can count frequent items as you insert them.

伪C解决方案:

int counts[1000000];

while(each number as n)
{
    counts[n]++;
    // then insert number into array
}

编辑# 2:确保将数组中的所有项目初始化为零,以免导致意外结果。

EDIT #2: Make sure, so you don't get unexpected results, to initialize all the items in the array to zero.

这篇关于如何获得最频繁的物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆