Java程序在数组列表中查找众数

标签 java

我创建了一个程序来查找模式。然后让它在括号中打印模式,如 "1 3 [5] 4 [5]" 但当数组列表中没有模式时,它会将第一个值声明为模式,如 “[1] 3 4 5”。如果没有模式,我不希望它在第一个整数上显示括号。

public static int mode(int[] array) {
    int mode = array[0];
    int maxCount = 0;
    for (int i = 0; i < array.length; i++) {
        int value = array[i];
        int count = 1;
        for (int j = 0; j < array.length; j++) {
            if (array[j] == value)
                count++;
            if (count > maxCount) {
                mode = value;
                maxCount = count;
            }
        }
    }
    return mode;
}

然后我这样打印:

int[] array = ...
int mode = mode(array);
boolean first = true;
for (int elt : array) {
    // print separator unless it's the first element
    if (first) {
        first = false;
    } else {
        System.out.print(' ');
    }
    if (elt == mode) {
        System.out.print(elt);
    } else {
        System.out.print('[');
        System.out.print(elt);
        System.out.print(']');
    }
}
System.out.println();

最佳答案

由于您的函数 mode() 默认返回数组中的初始元素作为默认模式,因此您无法判断该元素是模式还是根本没有模式的情况。因此,您可以对函数稍作更改,以在没有模式时返回 0,然后您的代码将如下所示:

class TestMode
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] array = {1,3,2,4,5};
        int mode = mode(array);
        for (int e : array) {
            if ((mode!=0) && (e==mode)) {
                System.out.print ("["+e+"]");
            }
            else {
                System.out.print(e);
            }
            System.out.print(" ");
        }
    }

    public static int mode(int[] array) {
        int mode = array[0];
        int maxCount = 0;
        for (int i = 0; i < array.length; i++) {
            int value = array[i];
            int count = 0;
            for (int j = 0; j < array.length; j++) {
                if (array[j] == value) count++;
                if (count > maxCount) {
                    mode = value;
                    maxCount = count;
                    }
                }
        }
        if (maxCount > 1) {
            return mode;
        }
        return 0;
    }
}

编辑:以下是返回真实模式集的函数:

public static Set<Integer> mode2(List<Integer> list) {
    int maxFrequency = 0;
    boolean modeFound = false;
    Set<Integer> modeSet = new HashSet<>();
    Collections.sort(list);
    for (int i=0; i<list.size(); i++) {
        int number = list.get(i);
        int count = 1;
        for (; (i+count)<list.size() && list.get(i+count)==number; count++) {}
        i+=(count-1);
        if (maxFrequency!=0 && count!=maxFrequency) {
            modeFound = true;
        }
        if (count > maxFrequency) {
            modeSet.clear();
            modeSet.add (number);
            maxFrequency = count;
        }
        else if (count == maxFrequency) {
            modeSet.add(number);
        }
    }
    if (!modeFound) {
        modeSet.clear();
    }
    return modeSet;
}

关于Java程序在数组列表中查找众数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36416048/

相关文章:

java - 使用 Jython 编译应用程序

java - 如何回滚除异常之外的所有内容以存储在表中

java - 使用 ProcessBuilder 通过命令行选项执行 python 脚本

java - 如何更改cordova子项目使用的java版本

java - JAVA中关联类的实现

java - 请帮助我处理以下代码?(JAVA)

java - 组织.apache.jasper.JasperException : Unable to compile class for JSP:

java - 在Java中打印出仅包含特定字符串的文本文件的每一行?

java - 如何修改 NDK 中的 Android 位图以便我可以在 Java 端使用它?

java - 在 Activity 之间传递 ArrayList<String[]>