如何找到单词流中最频繁出现的单词? [英] How to find the most frequent word in a word stream?

查看:95
本文介绍了如何找到单词流中最频繁出现的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在数组中查找最大重复元素

Possible Duplicate:
Finding the max repeated element in an array

如果存在一个单词流,并且一个单词的出现率达到51%或更高,那么如果仅是字符串,我们如何找到最频繁的单词,并且一次可以将int存储在内存中以帮助我们找到它.

If there is a word stream, with one word having an occurrence rate of 51% or more, how can we find the most frequent word if only string, and an int can be stored in the memory at a time to help us find it.

我们只能一次访问每个单词,因为这是一个流.

We can only access each word a single time, since this is a stream.

不需要特定的语言,但这主要是出于Java的考虑.

No specific language is necessary, but this is mainly intended with Java in mind.

我也不是要代码,只是问个主意. :)

Also I'm not asking for code, just the idea. :)

推荐答案

为完整起见,在Java中实施

For completeness, implementation in Java of the algorithm presented in the duplicate I pointed at, applied to an example:

public static void main(String[] args) throws InterruptedException {
    List<String> list = Arrays.asList("a", "b", "c", "a", "d", "e", "a", "a", "b", "b", "a");
    int counter = 0;
    String mostFrequentWord = "";
    for (String streamed : list) {
        if (streamed.equals(mostFrequentWord)) {
            counter++;
        } else if (counter == 0) {
            mostFrequentWord = streamed;
            counter = 1;
        } else {
            counter--;
        }
    }
    System.out.println(mostFrequentWord);
}

这篇关于如何找到单词流中最频繁出现的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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