Java ArrayList-搜索最常见的整数

Java ArrayList-搜索最常见的整数,java,arraylist,Java,Arraylist,我的arrayList中有999个数字,其中一些数字是重复的。我想找出列表中最频繁的数字,最有效的方法是什么?是的,慢慢来 你可以用一个列表来完成这项工作;内部列表包含您看到的数字,外部列表的索引是出现的次数。所以在处理“1,2,1,3,1,2,3,4”之后 [ [4], [2, 3], [1] ] 处理完输入列表后,您可以获得外部列表的最高索引所包含的最后一个内部列表,在本例中为[1]。该列表中的所有元素按最大出现次数排列。对列表进行排序,然后通过读取排序后的列表来计算出现次数最多的元素 需

我的arrayList中有999个数字,其中一些数字是重复的。我想找出列表中最频繁的数字,最有效的方法是什么?

是的,慢慢来

你可以用一个列表来完成这项工作;内部列表包含您看到的数字,外部列表的索引是出现的次数。所以在处理“1,2,1,3,1,2,3,4”之后

[ [4], [2, 3], [1] ]

处理完输入列表后,您可以获得外部列表的最高索引所包含的最后一个内部列表,在本例中为
[1]
。该列表中的所有元素按最大出现次数排列。

对列表进行排序,然后通过读取排序后的列表来计算出现次数最多的元素

需要0(n日志n)时间

分类

1 1 1 3 3 6 11 42 42 42 42 82

从左到右阅读列表,记住到目前为止看到最多的值和频率

正如您在评论中所写,我假设您读取的数字是从0到100的
从文本文件中,以便您可以使用

int[] count = new int[101];
...
count[numberJustRead]++;
...
看完所有的数字之后

int max = 0;
int maxIndex = 0; //this is what you looking for
for(int i = 0, k = count.length; i < k; i++){
  if(count[i] > max){
    max = count[i];
    maxIndex = i;
  }
}
int max=0;
int maxIndex=0//这就是你要找的
for(int i=0,k=count.length;i最大值){
max=计数[i];
maxIndex=i;
}
}

或者您可能喜欢guava的

这里有两个简单的实现,它们的复杂性不同(当然,如果您只有几个数字,那么性能增益是象征性的):

import java.util.*;
公开课考试
{
静态AbstractMap.SimpleEntry getMostFrequentN2(ArrayList值)
{
ArrayList频率=新的ArrayList();
int maxIndex=0;
主要内容:
对于(int i=0;i最大getValue())
{
max=新的AbstractMap.SimpleEntry(当前,计数);
}
电流=tmp.get(i);
计数=1;
}
}
如果(计数>最大getValue())
{
max=新的AbstractMap.SimpleEntry(当前,计数);
}
返回最大值;
}
公共静态void main(字符串[]args)
{
ArrayList编号=新的ArrayList(99);
对于(int i=0;i<99;++i)
{
add((int)(Math.random()*10));
}
系统输出打印项次(数字);
系统输出打印LN(getMostFrequentN2(数字));
System.out.println(getMostFrequentNLogN(数字));
}
}

不使用hashmap是什么意思?二元搜索树对您有好处吗?值的范围已知吗?您当然可以在不使用哈希映射的情况下找到它,但您想实现什么?如果是效率,那么数字的分布是什么?您需要提供更多信息。是的,我从文本文件中获取了范围为0到100的所有整数。我建议检查以下问题:[“k”最常见数字的算法][1][1]:
int max = 0;
int maxIndex = 0; //this is what you looking for
for(int i = 0, k = count.length; i < k; i++){
  if(count[i] > max){
    max = count[i];
    maxIndex = i;
  }
}
import java.util.*;

public class Test
{
    static AbstractMap.SimpleEntry<Integer, Integer> getMostFrequentN2(ArrayList<Integer> values)
    {
        ArrayList<AbstractMap.SimpleEntry<Integer, Integer>> frequencies = new ArrayList<>();

        int maxIndex = 0;

        main:
        for (int i = 0; i < values.size(); ++i)
        {
            int value = values.get(i);

            for (int j = 0; j < frequencies.size(); ++j)
            {
                if (frequencies.get(j).getKey() == value)
                {
                    frequencies.get(j).setValue(frequencies.get(j).getValue() + 1);

                    if (frequencies.get(maxIndex).getValue() < frequencies.get(j).getValue())
                    {
                        maxIndex = j;
                    }

                    continue main;
                }
            }

            frequencies.add(new AbstractMap.SimpleEntry<Integer, Integer>(value, 1));
        }

        return frequencies.get(maxIndex);
    }

    static AbstractMap.SimpleEntry<Integer, Integer> getMostFrequentNLogN(ArrayList<Integer> values)
    {
        ArrayList<Integer> tmp = new ArrayList(values);

        Collections.sort(tmp);

        AbstractMap.SimpleEntry<Integer, Integer> max = new AbstractMap.SimpleEntry<>(0, 0);

        int current = tmp.get(0);
        int count = 0;
        for (int i = 0; i < tmp.size(); ++i)
        {
            if (tmp.get(i) == current)
            {
                count++;
            }
            else
            {
                if (count > max.getValue())
                {
                    max = new AbstractMap.SimpleEntry<Integer, Integer>(current, count); 
                }

                current = tmp.get(i);

                count = 1;
            }
        }

        if (count > max.getValue())
        {
            max = new AbstractMap.SimpleEntry<Integer, Integer>(current, count); 
        }

        return max;
    }

    public static void main(String[] args)
    {
        ArrayList<Integer> numbers = new ArrayList(99);

        for (int i = 0; i < 99; ++i)
        {
            numbers.add((int)(Math.random() * 10));
        }

        System.out.println(numbers);

        System.out.println(getMostFrequentN2(numbers));
        System.out.println(getMostFrequentNLogN(numbers));
    }
}