0

I have an ArrayList<String> with one column which is data time seconds etc.

I want to find max number of repetitions for particular date and time(without seconds). It could be, e.g. 10 values with following parameters 23.12.2012 21:00:. So it must be stated that, say, take only 15 symbols on the left to compare.

The point is that I dont have a value to compare to, because I am not looking for particular value, I am looking for max number of repetitions and of course the value itself* which gave max number of repetitions. ArrayList is sorted in descending order, earliest date first, latest last. ArrayList size is quite big, 170 million raws.

So, how can I approach this task?

Thanks

4

1 回答 1

2

这个想法很简单。我们将利用ArrayList<String>已排序的事实。我们将线性扫描列表。我们将跟踪当前项目的计数。当项目更改为不同的项目时,我们将当前计数与目前看到的最大计数进行比较。如果它更大,我们用我们正在跟踪的项目替换最大计数和最大项目,然后重新开始计数。我会打电话给你的ArrayList<String> list

ArrayList<String> list; 
String currentMax; 
int maxCount = 0;
String current;
int count = 0;
for(int i = 0; i < list.size(); i++) {
    String item = parse(list.get(i));
    if(item.equals(current)) {
        count++;
    } 
    else { 
        if(count > maxCount) { 
            maxCount = count; 
            currentMax = current; 
        }
        count = 1;
        current = item;
    }
}

此外,您需要编写parse例程以String将列表中的 s映射ArrayList<String>到您要考虑的具有日期和时间但不具有秒数的部分。如果您的字符串采用格式"dd.mm.yyyy hh:mm*",则实现实际上非常简单:

static int length = "dd.mm.yyyy hh:mm".length();
static String parse(String item) {
    return item.substring(0, length);
}
于 2013-08-01T13:14:13.433 回答