java - 从数组中查找出现次数最多的 3 个整数

标签 java

我想知道如何找到数组中出现次数最多的 3 个整数。我读取了一个文件,其中包含整数,然后将其传递给一个数组,这就是我所拥有的。

public static void main(String[] args) {
    int[] num = readFiles("PATH FILE");
    System.out.println(Arrays.toString(num));
}
public static int[] readFiles(String file){

    try{
        File fl = new File(file);
        Scanner scan = new Scanner(fl);
        int ctr = 0;
        while(scan.hasNextInt()){
            ctr++;
            scan.nextInt();
        }
        int[] array1 = new int[ctr];

        Scanner scan1 = new Scanner(fl);

        for(int i = 0;i< array1.length;i++)
            array1[i] = scan1.nextInt();
        return array1;
    }
    catch(Exception e){
        return null;
    }
}}

最佳答案

参见https://stackoverflow.com/a/1852705/6496271

可以修改为查找前3名(所有数据均以m计算并存储)

关于java - 从数组中查找出现次数最多的 3 个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033842/

相关文章:

Java Servlet 和其他项目引用

java - 为什么输出存在差异

java - 获取mongodb java中最后插入的文档

java - 在 Eclipse 中使用 Maven 设置 GWT 项目

java - 将CSV文件解析为来自java中非常大的源的数组

java - 将字符串按数字分割并写入java中的输出文件

java - 如果我们在生成列表中同时提供两者,为什么 RESTful Web 服务选择 XML 而不是 JSON?

java - 如何分析 PermGen 内容?

java - 将用于Spring Boot应用程序的库放在额外的lib目录中

java - Android AWS S3 Upload TransferUtility错误上传部分中断: time out and socket is closed