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,676,767 Responses

  1. Richardaudit表示:

    https://clomfastpharm.shop/# can i buy clomid

  2. LanceFen表示:

    amoxicillin 500mg price canada: cost of amoxicillin 875 mg – amoxicillin 1000 mg capsule

  3. Sazrsmx表示:

    Выгодно заказать диплом университета!
    Приобрести диплом ВУЗа по выгодной цене возможно, обратившись к надежной специализированной компании. Приобрести диплом: diplomers.com/ofitsialnij-diplom-s-reestrom-garantiya-podlinnosti

  4. Gates of Olympus oyununda kayıplar yaşıyorum!
    dede oyunu para kazanma, Pragmatic Play tarafından sunulan sürükleyici bir slot oyunudur.|Pragmatic Play’in sunduğu Gates of Olympus, tanrısal bir temaya sahiptir. Zeus’un dünyasına giriş yaparak büyük kazançlar elde edebilirsiniz.

    Gates of Olympus demo oyna seçeneği ile oyunun temposunu ve özelliklerini önceden deneyimleyebilirsiniz.

    Gates of Olympus oyna demo modunda, oyunun farklı özelliklerini ve kazanç sistemini deneyimleyebilirsiniz. Oyunun ana özellikleri arasında dinamik ödeme sistemi ve büyük ödüller bulunur. Gates of Olympus ile eğlencenin ve kazancın kapılarını aralayın!

  5. Michaelscoob表示:

    ZithPharmOnline: cost of generic zithromax – ZithPharmOnline

  6. LanceFen表示:

    Clom Fast Pharm: clomid pill – Clom Fast Pharm

  7. Hi, І do believe this is a greɑt website. I stumbledupon іtt 😉 I’m going
    to return yet again since I boօk marked it. Money and freeⅾom is the best way to change,
    may you be rich and continue to help other people.

    Feell free ttߋ visit mmy ᴡeb-site – sewa boom lift gresik

  8. esta usa visa表示:

    This post is worth everyone’s attention. Where can I find out more?

  9. I am now not positive the place you’re getting your information, however good topic.
    I must spend some time finding out much more or figuring out more.
    Thanks for great info I used to be looking for this info for
    my mission.

  10. Michaelscoob表示:

    cheap clomid online: Clom Fast Pharm – how to buy clomid without rx

  11. BrianTom表示:

    price of amoxicillin without insurance over the counter amoxicillin canada AmOnlinePharm

  12. Click here表示:

    Thanks for finally talking about > JS 依照元素出現數量排序
    – 馬老師 雲端研究室 < Liked it!

  13. Michaelscoob表示:

    purchase zithromax z-pak: ZithPharmOnline – ZithPharmOnline

  14. Hey there! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly? My web site looks weird when browsing
    from my iphone4. I’m trying to find a theme or plugin that might be able to resolve this
    issue. If you have any recommendations, please share. Thanks!

  15. Danieldrymn表示:

    ZithPharmOnline: ZithPharmOnline – ZithPharmOnline

  16. Richardaudit表示:

    http://amonlinepharm.com/# AmOnlinePharm

  17. Thomasbiz表示:

    онлайн казино на андроид – лучшие онлайн казино, онлайн казино на андроид

  18. Pingidiof表示:

    darknet websites https://github.com/aresonioncq0a7/aresonion – darknet markets 2025

  19. Richardaudit表示:

    https://zithpharmonline.shop/# zithromax for sale online

  20. Great information. Lucky me I ran across your website by accident
    (stumbleupon). I have saved as a favorite for later!

  21. Michaelscoob表示:

    Clom Fast Pharm: Clom Fast Pharm – Clom Fast Pharm

  22. Hello there! I simply want to offer you a big thumbs up for the excellent information you have right
    here on this post. I am returning to your blog for
    more soon.

  23. BrianTom表示:

    can i buy cheap clomid price Clom Fast Pharm how to get generic clomid without insurance

  24. DonDonrob表示:

    darknet drug store https://github.com/nexusdarkrtv1u/nexusdark – darknet drug market

  25. OliverSat表示:

    взгляните на сайте здесь Mango-Office система автоответа

  26. Michaelscoob表示:

    amoxicillin 500mg price in canada: buy amoxicillin without prescription – AmOnlinePharm

  27. Richardaudit表示:

    https://clomfastpharm.com/# Clom Fast Pharm

  28. esta usa visa表示:

    What a information of un-ambiguity and preserveness of precious know-how
    concerning unpredicted feelings.

  29. Richardaudit表示:

    https://predpharmnet.shop/# buy prednisone with paypal canada

發佈留言

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