查找数组中出现频率最高的值,如果有平局,则选择最低值

我有以下程序查找数组“a”,然后输出数组中出现频率最高的值。然而,我想实现的另一个条件是,在两个不同值出现相同次数的情况下,最低值获得输出。


因此,对于以下带有数组的代码:


int a[] = {34, 34, 20, 20, 15};

它输出 34 但我希望它输出 20,因为这是一个较低的值并且在数组中出现的次数相同。


public class Arrays3 {

    public static void main(String[] args){

        int a[] = {34, 34, 20, 20, 15};

        mode(a);

    }

    public static int mode(int[] a) {

        int[] counts = new int[101];

        int maxCount = 0;

        int maxKey = 0;


        for(int i = 0; i < a.length; i++) {

            counts[a[i]]++;

            if(counts[a[i]] > maxCount) {

                maxCount = counts[a[i]];

                maxKey = a[i];

            }

        }

        System.out.println(maxKey);

        return maxKey;

    }

}


温温酱
浏览 228回答 3
3回答

拉风的咖菲猫

您可以检查maxKey并执行以下操作:&nbsp;if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {&nbsp; &nbsp; &nbsp; &nbsp;maxKey = counts[a[i]];&nbsp;}因此,如果有平局,maxKey则将设置为较小的元素。然后 ifcount[a[i]]永远大于maxCount,maxKey将被覆盖并成为最常出现的元素:for(int i = 0; i < a.length; i++) {&nbsp; &nbsp; &nbsp;counts[a[i]]++;&nbsp; &nbsp; &nbsp;if(counts[a[i]] > maxCount) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxCount = counts[a[i]];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxKey = a[i];&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {&nbsp; &nbsp; &nbsp; &nbsp; maxKey = a[i];&nbsp; &nbsp; &nbsp; }}System.out.println(a[maxKey]);输出20

Qyouu

一个有趣的解决方案(我写得很开心)是首先Map通过流式传输数组并使用Collectors.groupingBywith来创建频率Collectors.counting()。然后,我们可以流式传输并Collectors.groupingBy再次使用来创建一个Map<Long, SortedSet<Integer>>(键是频率,值是数组中具有该频率的排序值集)。最后,我们可以对 进行排序,Map以便最高频率出现在最前面,然后简单地从 中获取最低的元素SortedSet:int[] a = {34, 34, 20, 20, 15};var lowest = Arrays.stream(a)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.boxed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.entrySet()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.groupingBy(Map.Entry::getValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collectors.mapping(Map.Entry::getKey, Collectors.toCollection(TreeSet::new))))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.entrySet()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.sorted(Comparator.comparing(Map.Entry::getKey, Comparator.reverseOrder()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(Map.Entry::getValue)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToInt(TreeSet::first)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.findFirst();lowest.ifPresent(System.out::println);输出:20

慕斯709654

你想寻求最小值,所以我建议 put int maxKey = Integer.MAX_VALUE;然后每次找到更好的counts,就必须比较更新maxKey,毕竟解决方案是这样的int[] counts = new int[101];int maxCount = 0;int maxKey = Integer.MAX_VALUE;for(int i = 0; i < a.length; i++) {&nbsp; &nbsp; counts[a[i]]++;&nbsp; &nbsp; if(counts[a[i]] >= maxCount) {&nbsp; &nbsp; &nbsp; &nbsp; maxCount = counts[a[i]];&nbsp; &nbsp; &nbsp; &nbsp; if(a[i] < maxKey) maxKey = a[i];&nbsp; &nbsp; }}System.out.println(maxKey);return maxKey;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java