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,467,004 Responses

  1. Hi, I do believe this is a great blog. I stumbledupon it 😉 I’m going to revisit
    once again since i have book marked it. Money and freedom is the best way to change,
    may you be rich and continue to guide others.

  2. Lillie表示:

    I don’t even know how I ended up right here, but I thought this publish was great.
    I don’t recognise who you’re however certainly you’re going to a famous blogger if you happen to aren’t already.
    Cheers!

  3. All The Comforts Of Home – Or House! 에볼루션 카지노 가입 – evolutionkr.kr

  4. This was quite informative. For more, visit frenzy char dham yatra

  5. sex pha trinh表示:

    Thank you for sharing your info. I really appreciate your efforts and I am waiting for your next write ups thank you once
    again.

  6. Lily Gordon表示:

    Great insights! Find more at massage spa

  7. Emilie Castro表示:

    Thanks for the great information. More at roofing near me

  8. Terrific article! That is the kind of information that are supposed
    to be shared across the internet. Disgrace on the search engines for
    now not positioning this publish higher! Come on over and visit my web site .
    Thank you =)

  9. Trina表示:

    10 Websites To Help You Learn To Be An Expert In American Fridge Freezers american-style
    fridge [Trina]

  10. It’s an amazing piece of writing in support of all the web viewers; they will
    obtain benefit from it I am sure.

  11. JasonUnsoG表示:

    http://slotsiteleri25.com/# en cok kazand?ran slot oyunlar?

  12. Thanks for your marvelous posting! I definitely enjoyed reading it,
    you happen to be a great author. I will ensure that I bookmark your blog and
    may come back later on. I want to encourage continue your great work,
    have a nice evening!

  13. The simplicity of the interface combines with versatile
    opportunities, customizable for {you must|you will have to be very|very|extremely|extremely careful | circumspect when choosing| searching for
    a gambling establishment| platform.

    Feel free to surf to my page :: https://www.stepharmaceuticals.com/2025/01/08/top-5-books-about-primexbt-cryptocurrency/

  14. 김포쪽에 급전 필요 하신분 있으신가요? 김포 노래방알바에서 해결 해드립니다.

  15. JasonUnsoG表示:

    http://casinositeleri25.com/# deneme bonusu veren casino siteleri

  16. 12 Companies Are Leading The Way In Asbestos Attorney Mesothelioma attorneys

  17. Wallacefex表示:

    yeni deneme bonusu veren siteler: deneme bonusu veren yeni siteler – deneme bonusu veren yeni siteler

  18. Brent Graham表示:

    This was very beneficial. For more, visit Roofing Educators

  19. Slot Thailand dengan daftar link slot server thailand super gacor no 1 di situs ini anti rungkad gam pang jackpot dengan menyediakan aku pro super thailand.

  20. Leo Horton表示:

    Excellent learn! I can’t wait to try out a number of those hair salons in West Vancouver stylish for your assistance. More alternate options will also be stumbled on at hair stylist

  21. Trial ED Set Extreme online without prescription canada. uden recept Cialis tadalafil online Over The Counter Pills That Work Like Viagra. Imovane, imovane uden recept, dele imovane 5 mg Chignik. Get directions to Ruths Chris. Imovane apoteket, imovane uten resept, imovane 7.5mg pret, imovane 7.5. https://medicinafgivelse.store/amilco/ Bestille Amilco Billig

  22. Wallacefex表示:

    deneme bonusu veren siteler yeni: deneme bonusu veren siteler – deneme bonusu veren siteler

  23. My brother suggested I might like this blog. He was totally right.
    This post actually made my day. You cann’t imagine simply how much time I had
    spent for this info! Thanks!

  24. Ruth Barrett表示:

    A estética dental é um assunto muito interessante! Quero saber mais sobre isso na minha próxima consulta à Ortodontia

  25. JasonUnsoG表示:

    https://casinositeleri25.com/# guvenilir casino siteleri

發佈留言

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