猿问

在数组中找到最常见的元素并返回新数组

我正在尝试编写一个方法: 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 {}


繁花如伊
浏览 162回答 2
2回答

元芳怎么了

我试图根据您的知识为您提供答案,根据样本,最大数字为 16,想法是用 16 个数字 0 填充一个数组,我们将使用索引,我们将遍历该数组,在第一次迭代中,index 的值将是 1,然后对于它的每次迭代,我们将询问在我们的数字数组中是否存在该值,如果存在,我们将为我们的 16 个值为 0 的数字数组添加 +1,术语为过程会将值 0 替换为与索引匹配的数组编号的重合次数,您只需完成编码,使其返回重复次数最多的数字,null 或无。如果有必要,我可以完成它的编码,但重要的是你了解这个过程并开始玩循环、问候和你告诉我的任何事情。public class main {&nbsp; &nbsp; static Integer [] numeros = {1,2,1,3,4,4};&nbsp; &nbsp; static Integer[] arr = Collections.nCopies(16, 0).toArray(new Integer[0]);&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <arr.length ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j <numeros.length ; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i<numeros.length){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(numeros[j] == i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i]=arr[i]+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <arr.length ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("count of&nbsp; numbers " + i + " :&nbsp; "&nbsp; + arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }输出:count of&nbsp; numbers 1 :&nbsp; 2count of&nbsp; numbers 2 :&nbsp; 1count of&nbsp; numbers 3 :&nbsp; 1count of&nbsp; numbers 4 :&nbsp; 2................

MMTTMM

这是该练习的一种解决方案,尽管您可能无法使用它,甚至无法理解它,因为它使用了您可能尚未学习的 Java 特性,例如可变参数、流和方法引用。static int[] moreCommon(int... a) {&nbsp; &nbsp; if (a == null || a.length == 0)&nbsp; &nbsp; &nbsp; &nbsp; return a;&nbsp; &nbsp; return Arrays.stream(a).boxed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here we have Map<Integer, Long> mapping value to frequency&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .entrySet().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Entry::getValue, TreeMap::new,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.mapping(Entry::getKey, Collectors.toList())))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here we have TreeMap<Long, List<Integer>> mapping frequency to list of values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .lastEntry().getValue().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // here we have Stream<Integer> streaming most frequent values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .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[]
随时随地看视频慕课网APP

相关分类

Java
我要回答