Java 查找数组中最常见的元素并返回新数组

Java 查找数组中最常见的元素并返回新数组,java,arrays,Java,Arrays,我正在尝试编写一个方法:static int[]moreCommon int[]a MoreComon方法接受一个int数组作为其唯一参数,并返回一个包含所有具有最大频率的值的具有适当长度的整数数组 例如: if a={1, 16, 10, 4, 16, 5, 16} it returns {16}; if a={1, 16, 10, 1, 16, 1, 16} it returns {1,16}; if a=null it returns null; if a={} it return

我正在尝试编写一个方法:static int[]moreCommon int[]a

MoreComon方法接受一个int数组作为其唯一参数,并返回一个包含所有具有最大频率的值的具有适当长度的整数数组

例如:

if a={1, 16, 10, 4, 16, 5, 16} it returns {16};    
if a={1, 16, 10, 1, 16, 1, 16} it returns {1,16};
if a=null it returns null;
if a={} it returns {}

这里是这个练习的一个解决方案,尽管您可能无法使用它,甚至无法理解它,因为它使用了您可能还没有学会的Java特性,例如varargs、streams和方法引用

static int[] moreCommon(int... a) {
    if (a == null || a.length == 0)
        return a;
    return Arrays.stream(a).boxed()
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
            // here we have Map<Integer, Long> mapping value to frequency
            .entrySet().stream()
            .collect(Collectors.groupingBy(Entry::getValue, TreeMap::new,
                        Collectors.mapping(Entry::getKey, Collectors.toList())))
            // here we have TreeMap<Long, List<Integer>> mapping frequency to list of values
            .lastEntry().getValue().stream()
            // here we have Stream<Integer> streaming most frequent values
            .mapToInt(Integer::intValue).sorted().toArray();
}
输出

[16] [1, 16] 无效的 []
根据你的知识,我试着给你一个答案,根据样本,最大数字是16,想法是用16个数字0填充一个数组,我们将使用索引,我们将遍历该数组,在第一次迭代中,索引的值将是1,然后,对于每个迭代,我们将询问在我们的数字数组中是否存在值,如果存在,我们将向16个数字的数组中添加+1,值为0,这个过程将替换这些值​​0乘以与索引匹配的数组数的重合数, 您只需完成编码,使其返回重复次数最多的数字,null或nothing。如果有必要,我可以完成编码,但重要的是你要理解这个过程,并开始玩循环,问候语和你告诉我的任何事情

public class main {

    static Integer [] numeros = {1,2,1,3,4,4};
    static Integer[] arr = Collections.nCopies(16, 0).toArray(new Integer[0]);

    public static void main(String[] args) {

        for (int i = 1; i <arr.length ; i++) {
            for (int j = 0; j <numeros.length ; j++) {
                if(i<numeros.length){
                    if(numeros[j] == i){
                        arr[i]=arr[i]+1;
                    }
                }
            }
        }
        for (int i = 1; i <arr.length ; i++) {
            System.out.println("count of  numbers " + i + " :  "  + arr[i]);
                 }
             }
        }

请展示您的尝试:要求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作的摘要,以及您解决问题的困难的描述。如果您在开始时遇到困难,我建议您首先考虑如何存储元素及其频率,那么如何遍历列表并提取这些信息。非常感谢您的回答和时间。然而,我不确定这是否是程序所要求的!输出应该是包含所有最常见数字的和数组。例如,ifa={1,2,1,3,2,5}它应该返回{1,2}这是最常见的数字。非常感谢您的回答!你是对的,不幸的是我仍然不能理解这个代码。。不过还是谢谢你
public class main {

    static Integer [] numeros = {1,2,1,3,4,4};
    static Integer[] arr = Collections.nCopies(16, 0).toArray(new Integer[0]);

    public static void main(String[] args) {

        for (int i = 1; i <arr.length ; i++) {
            for (int j = 0; j <numeros.length ; j++) {
                if(i<numeros.length){
                    if(numeros[j] == i){
                        arr[i]=arr[i]+1;
                    }
                }
            }
        }
        for (int i = 1; i <arr.length ; i++) {
            System.out.println("count of  numbers " + i + " :  "  + arr[i]);
                 }
             }
        }
count of  numbers 1 :  2
count of  numbers 2 :  1
count of  numbers 3 :  1
count of  numbers 4 :  2
........
.....
...