【问题标题】:find most common element in array and return new array在数组中找到最常见的元素并返回新数组
【发布时间】:2018-11-13 21:29:45
【问题描述】:

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

方法moreCommon 接受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 {}

【问题讨论】:

  • 请展示你的尝试
  • 来自help center:请求作业帮助的问题必须包括您迄今为止为解决问题所做的工作的总结,以及对困难的描述你正在解决它。
  • 如果您在开始时遇到困难,我建议您首先考虑如何将元素与其频率一起存储,然后如何遍历列表并提取此信息。

标签: java arrays


【解决方案1】:

这是练习的一种解决方案,尽管您可能无法使用它,甚至无法理解它,因为它使用了您可能尚未了解的 Java 功能,例如可变参数、流和方法引用。

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();
}

测试

System.out.println(Arrays.toString(moreCommon(1, 16, 10, 4, 16, 5, 16)));
System.out.println(Arrays.toString(moreCommon(1, 16, 10, 1, 16, 1, 16)));
System.out.println(Arrays.toString(moreCommon(null)));
System.out.println(Arrays.toString(moreCommon()));

输出

[16]
[1, 16]
null
[]

【讨论】:

  • 非常感谢您的回答!你是对的,不幸的是我仍然无法理解这段代码。但无论如何谢谢
【解决方案2】:

我已尝试根据您的知识为您提供答案,根据样本,最大数字为 16,想法是用 16 个数字 0 填充数组,我们将使用索引,我们将通过该数组,在第一次迭代中,索引的值将为 1,然后对于它的每次迭代,我们将询问在我们的数字数组中是否存在该值,如果存在,我们将 +1 添加到我们的 16 个数字数组,值为 0 , 术语此过程将用与索引匹配的数组编号的重合次数替换值 0, 你只需要完成它的编码,让它返回重复次数最多的数字,null 或什么都没有。如果有必要,我可以完成它的编码,但重要的是你了解这个过程并开始使用循环、问候和你告诉我的任何东西。

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
........
.....
...

【讨论】:

  • 非常感谢您的回答和您的时间。但是我不确定这是否是程序要求的!输出应该是包含所有最常见数字的数组。例如 ifa={ 1,2,1,3,2,5} 它应该返回 {1,2} 这是最常见的数字
猜你喜欢
  • 1970-01-01
  • 2019-10-16
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-25
相关资源
最近更新 更多