第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

查找數(shù)組中出現(xiàn)頻率最高的值,如果有平局,則選擇最低值

查找數(shù)組中出現(xiàn)頻率最高的值,如果有平局,則選擇最低值

溫溫醬 2021-12-10 15:02:10
我有以下程序查找數(shù)組“a”,然后輸出數(shù)組中出現(xiàn)頻率最高的值。然而,我想實(shí)現(xiàn)的另一個(gè)條件是,在兩個(gè)不同值出現(xiàn)相同次數(shù)的情況下,最低值獲得輸出。因此,對于以下帶有數(shù)組的代碼:int a[] = {34, 34, 20, 20, 15};它輸出 34 但我希望它輸出 20,因?yàn)檫@是一個(gè)較低的值并且在數(shù)組中出現(xiàn)的次數(shù)相同。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;    }}
查看完整描述

3 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗(yàn) 獲得超2個(gè)贊

您可以檢查maxKey并執(zhí)行以下操作:


 if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {

       maxKey = counts[a[i]];

 }

因此,如果有平局,maxKey則將設(shè)置為較小的元素。然后 ifcount[a[i]]永遠(yuǎn)大于maxCount,maxKey將被覆蓋并成為最常出現(xiàn)的元素:


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

     counts[a[i]]++;


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

          maxCount = counts[a[i]];

          maxKey = a[i];

      }

      if(counts[a[i]] == maxCount && counts[a[i]] < maxKey) {

        maxKey = a[i];

      }

}

System.out.println(a[maxKey]);

輸出


20


查看完整回答
反對 回復(fù) 2021-12-10
?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊

一個(gè)有趣的解決方案(我寫得很開心)是首先Map通過流式傳輸數(shù)組并使用Collectors.groupingBywith來創(chuàng)建頻率Collectors.counting()。


然后,我們可以流式傳輸并Collectors.groupingBy再次使用來創(chuàng)建一個(gè)Map<Long, SortedSet<Integer>>(鍵是頻率,值是數(shù)組中具有該頻率的排序值集)。


最后,我們可以對 進(jìn)行排序,Map以便最高頻率出現(xiàn)在最前面,然后簡單地從 中獲取最低的元素SortedSet:


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


var lowest = Arrays.stream(a)

                   .boxed()

                   .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))

                   .entrySet()

                   .stream()

                   .collect(Collectors.groupingBy(Map.Entry::getValue,

                           Collectors.mapping(Map.Entry::getKey, Collectors.toCollection(TreeSet::new))))

                   .entrySet()

                   .stream()

                   .sorted(Comparator.comparing(Map.Entry::getKey, Comparator.reverseOrder()))

                   .map(Map.Entry::getValue)

                   .mapToInt(TreeSet::first)

                   .findFirst();


lowest.ifPresent(System.out::println);

輸出:


20


查看完整回答
反對 回復(fù) 2021-12-10
?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

你想尋求最小值,所以我建議 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++) {

    counts[a[i]]++;

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

        maxCount = counts[a[i]];

        if(a[i] < maxKey) maxKey = a[i];


    }

}

System.out.println(maxKey);

return maxKey;


查看完整回答
反對 回復(fù) 2021-12-10
  • 3 回答
  • 0 關(guān)注
  • 228 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號