JS 依照元素出現數量排序

同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:

var array = ["1", "2", "4", "5", "2", "4", "2", "3"];
var str = array.toString();
var filter = [];
var result = [];
document.write("原始陣列:" + array + "<br>");

var get = function (str, tar, arr, tmp) {
    if (str.indexOf(tar) >= 0) {
        tmp = str.slice(str.indexOf(tar) + 1);
        arr.push(tar);
        get(tmp, tar, arr, tmp);
    }
    return arr;
}

for (i in array) {
    var elm = [];
    var tmp;
    var fstr = filter.join();
    if (fstr.indexOf(array[i]) >= 0) {
        continue;
    } else {
        var tmp_arr = get(str, array[i], elm, tmp);
        result.push(tmp_arr.length + ':' + tmp_arr[0]);
        filter.push(array[i]);
    }
}

result.sort();
result.reverse();

//按照出現次數排序,不刪除重複元素
var finishArray = [];

for (index in result) {
    var show = result[index].split(':');
    for (i = 0; i < show[0]; i++) {
        finishArray.push(show[1]);
    }
}
document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>");

//按照出現次數排序,刪除重複元素
var finishArray = [];
for (index in result) {
    var show = result[index].split(':');
    finishArray.push(show[1]);
}
document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");

輸出結果:

原始陣列:1,2,4,5,2,4,2,3
按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1
按出現次數排序,刪除重複元素:2,4,5,3,1

You may also like...

1,228,230 Responses

  1. It’s amazing designed for me to have a web site, which is useful for my knowledge.
    thanks admin

  2. See What Car Key Reprogramming Tricks The Celebs Are Utilizing car key reprogramming

  3. Darrelherse表示:

    2024 en iyi slot siteleri: deneme bonusu veren siteler – en cok kazandiran slot siteleri

  4. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza indir

  5. Darrelherse表示:

    canl? slot siteleri: yasal slot siteleri – slot casino siteleri

  6. Charline表示:

    The Reasons Car Key Programmer Near Me Is More Difficult Than You Imagine auto car key programmer near
    me (Charline)

  7. WilberCes表示:

    Sul mercato ci sono un sacco di alimentare formulato soprattutto per bambini Che vanno dai prodotti lattiero-caseari, come yogurt, al cibo tipico bambino, ai dolci come i biscotti o cereali https://rsmegane.com/proxy.php?link=https://womanityworld.com

  8. Keep this going please, great job!

  9. I like what you guys tend to be up too. Such clever work and reporting!
    Keep up the terrific works guys I’ve you guys to my blogroll.

  10. Darrelherse表示:

    oyun siteleri slot: guvenilir slot siteleri 2024 – en iyi slot siteler

  11. ev taşıma表示:

    Evden Eve Nakliyat | Müşteri memnuniyetinin önemini vurgulayan bu makaleyi büyük bir ilgiyle okuduk. Kozcuoğlu Evden Eve Nakliyat olarak, her zaman müşteri memnuniyetini ön planda tutuyoruz.

  12. Buzzwords, De-Buzzed: 10 Other Methods To Say Replacement Key For Renault Clio renault clio Key programming (http://tst.ezmir.co.kr/bbs/board.php?bo_table=qna&wr_id=183546)

  13. item635483390表示:

    I am extremely inspired along with your writing skills as well as with the layout on your weblog.

    Is that this a paid theme or did you customize it yourself?

    Anyway keep up the nice quality writing, it’s uncommon to look a nice weblog like this one today..

    my blog; item635483390

  14. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza giris

  15. Ι have ƅeen exploring foor a little bit foг any һigh-quality articles
    оr blog posts оn this ҝind of ɑrea . Exploring іn Yahoo I eventually stumbled սpon tyis
    web site. Studying tһіs info So i’m glad to exhibit that Ι’ve
    ɑn incredibly ցood uncanny feeling I discovered just ԝһаt
    I needed. Ӏ most indubitably wіll mɑke sure t᧐ do not ffail to remember
    thіs site and gіve it a ⅼook regularly.

    Feeel free tⲟ visit my bllg – PokemonToto Live Chat

  16. Darrelherse表示:

    en iyi slot siteleri: en iyi slot siteleri 2024 – slot siteleri guvenilir

  17. Ev Taşıma | Ev taşımak, doğru planlama yapıldığında çok daha kolay olabilir. Kozcuoğlu Ev Taşıma olarak, müşterilerimizin tüm planlama süreçlerinde yanlarında olmaktan gurur duyuyoruz.

  18. JamesscarP表示:

    השבוע. מה לעשות, זה מה שקורה כאשר חלומות מתגשמים. אתה רק צריך להזמין נערות ליווי ברמת גן, והן כבר יגשימו לך את החלומות. מידי תשוקה רבה היוצרת משיכה חזקה לבת הזוג. והדבר השלישי הוא זה שהגבר לומד דברים חדשים. כך הוא יכול להכניס גיוון לחיי המין עם בת their explanation

  19. KevinSounc表示:

    https://denemebonusuverensiteler.win/# bonus veren siteler

  20. avtoworld.lv表示:

    The Associated With Organic Traffic And Clicks
    백링크 업체 [avtoworld.lv]

  21. Darrelherse表示:

    deneme bonusu veren slot siteleri: en cok kazandiran slot siteleri – slot oyunlar? siteleri

  22. Pachinko combines traditional Japanese gaming with modern twists, offering a one-of-a-kind experience.

  23. KevinSounc表示:

    http://slotsiteleri.bid/# casino slot siteleri

  24. Darrelherse表示:

    casino slot siteleri: slot oyunlar? siteleri – deneme veren slot siteleri

  25. Simply wіsh to saʏ yoiur article іs as astonishing.
    Thе clearness in yourr post is simply spectacular аnd і can assume yoս aree an expert on this subject.

    Wеll wіth your permission lеt mе to grab үour feed
    to қeep ᥙp to ddate ᴡith forthcoming post.
    Ƭhanks a miⅼlion and рlease carry օn thee gratifying work.

    Αlso visit my web-site – PokemonToto Login

  26. at least, during the first 15 minutes in the https://lickystarcasino.in/ dialogue with almost no
    pauses.

  27. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza demo oyna

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。