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,362,991 Responses

  1. GLASS & PIPES表示:

    I like the helpful info you provide in your articles. I will bookmark your
    blog and check again here regularly. I am quite certain I will learn lots of new stuff
    right here! Best of luck for the next!

  2. RamonRek表示:

    buy semaglutide online: buy semaglutide online – buy semaglutide online

  3. 10 Tell-Tale Symptoms You Need To Get A New Leather Sofa With
    Chaise Faux leather sofa set

  4. Frankevild表示:

    https://rybelsus.shop/# rybelsus coupon

  5. Jeremypag表示:

    buy semaglutide pills cheapest rybelsus pills semaglutide cost

  6. 대출 금리表示:

    Fbar: Foreign Bank (And Financial) Account Reporting Rules Expanded With Higher Penalties 대출 금리

  7. Jeremypag表示:

    ozempic coupon ozempic ozempic

  8. Genuinely no matter if someone doesn’t be aware of after that its up to other
    people that they will assist, so here it takes place.

  9. eoopxrjda表示:

    Noun. Day light savings. Transylvania. Holocaust. Tyler perry movies. Their eyes were watching god. https://81.200.117.113

  10. gadgets表示:

    I am really impressed with your writing skills and also with the layout
    on your weblog. Is this a paid theme or did you modify it yourself?
    Anyway keep up the excellent quality writing, it is
    rare to see a great blog like this one today.

  11. Frankevild表示:

    https://ozempic.art/# ozempic cost

  12. Frankevild表示:

    https://ozempic.art/# ozempic cost

  13. Jeremypag表示:

    semaglutide online semaglutide cost rybelsus pill

  14. Mobility Scooter For Shopping Tools To Streamline Your Daily Lifethe One Mobility Scooter For Shopping Trick Every
    Individual Should Know mobility scooter For Shopping

  15. See What Best Lightweight Folding Electric Wheelchair Uk Tricks The Celebs Are Using folding electric wheelchair uk

  16. যখন আপনি শেষ ব্যক্তিগত অ্যালবাম, আপনার সকলের জন্য অপেক্ষা করছে একটি বিশাল আর্থিক পুরস্কার!

    Check out my web blog: https://parimatch-slots-bd.com/

  17. helpful hints表示:

    Sweet blog! I found it while surfing around on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Thank you

  18. HaroldBig表示:

    buy ozempic: buy cheap ozempic – buy cheap ozempic

  19. What Will Toto Be Like In 100 Years? 먹튀검증 사이트

  20. Jeremypag表示:

    ozempic ozempic online buy cheap ozempic

  21. Politely fellow commentator ,

    I unearthed the reflections shared in this blog post to be incredibly enlightening
    . The writer’s mastery of the subject is genuinely commendable.

    If you are desiring a thrilling and advantageous internet-based gaming adventure, I would sincerely
    convince you to uncover the possibilities of VIVARO CASINO.

    With its vast array of mesmerizing recreations, copious rewards , and hassle-free experience ,
    VIVARO CASINO facilitates an matchless recreational experience that appeals to
    both inexperienced and veteran enthusiasts in the same manner .

    I persuade you to explore VIVARO CASINO and reveal the adrenaline that is
    ready to be experienced you. I am certain you will perceive the
    experience to be highly rewarding.

    Kindest regards

    Here is my website … online gambling

  22. HaroldBig表示:

    rybelsus coupon: cheapest rybelsus pills – semaglutide online

  23. кроме того, вы получите возможность настроить переадресацию таким образом, чтобы прием
    вызовов распределялся между группой менеджеров.

    my homepage: Виртуальный номер для телеграм

  24. Initially, giving on your own a peptide injection at home may seem
    confusing or challenging.

  25. mnnyaxnvz表示:

    Color theory. Genetic testing. Kidney stone. Jonestown massacre. Christian bale movies. Omniscient. https://81.200.117.113

  26. JacobGed表示:

    sports betting bonus codes: These are bonuses and special offers provided by Bet at Home, which may include free bets, deposit matches, or enhanced odds on various sporting events.

發佈留言

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