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,970 Responses

  1. вывод из запоя стационар ростов вывод из запоя стационар ростов .

  2. BarryDof表示:

    http://rybelsus.shop/# rybelsus coupon

  3. This site was… how do you say it? Relevant!! Finally I’ve found something that helped me.
    Thanks!

  4. Jeremypag表示:

    ozempic cost Ozempic without insurance ozempic coupon

  5. xnxx bokep表示:

    Fastidious replies in return of this query with real arguments and telling all on the topic of that.

  6. Tara表示:

    You’ll Never Guess This SEO Company Manchester’s Secrets seo company manchester [Tara]

  7. From The Web: 20 Fabulous Infographics About Used Mobility Scooters For Sale Mobility scooter for sale near me

  8. Tilly Vessels表示:

    15 Documentaries That Are Best About Guy Sex Toys best sex toy for man, Douglas,

  9. Frankevild表示:

    http://ozempic.art/# buy ozempic pills online

  10. When some one searches for his required thing, thus he/she needs to be available that in detail,
    so that thing is maintained over here.

  11. The Reasons To Focus On The Improvement Of Heavy Duty Mobility Scooters High-performance Scooters

  12. Jeremypag表示:

    semaglutide online rybelsus coupon semaglutide tablets

  13. web表示:

    You really make it appear so easy along with your presentation however I find this matter to be
    really something which I believe I’d by no means understand.
    It seems too complicated and extremely large for me.
    I’m looking forward to your subsequent submit, I’ll try to get the dangle
    of it!

  14. Jeremypag表示:

    cheapest rybelsus pills rybelsus coupon semaglutide cost

  15. Need Inspiration? Check Out Espresso Coffee Maker best ground coffee for Espresso machine

  16. 5 Killer Quora Answers To Cost For Spare Car Key cost For spare car key (https://willysforsale.com/author/dealtower14)

  17. RamonRek表示:

    buy rybelsus online: buy semaglutide pills – buy semaglutide pills

  18. Tobias Sun表示:

    3 Wheel Scooter For Handicapped 101 A Complete Guide For Beginners 3 wheel scooter for sale near me (egholm-Ritter.hubstack.net)

  19. Frankevild表示:

    https://rybelsus.shop/# rybelsus pill

  20. BarryDof表示:

    https://ozempic.art/# buy cheap ozempic

  21. Nine Things That Your Parent Taught You About Remote Key Repair remote Key Repair

  22. WilliamPeemo表示:

    Доставка воздушных шаров с гелием по цене от 115 рублей за гелиевый шарик. Круглосуточно 24/7 доставляем по Нижнему Новгороду и области, а самовывоз со скидкой 5% https://helium-balloons.ru

  23. The 10 Most Terrifying Things About Titration For ADHD titration For adhd

  24. Judi Lehman表示:

    10 Things Everyone Has To Say About Citroen Key Citroen Key citroen key Fob

  25. 8 Tips To Up Your Toto Online Terbaik Game Togel Hongkong

  26. This Is The Ugly Truth About Rabbit Vibrators bunny ears sex toy

  27. Frankevild表示:

    http://rybelsus.shop/# semaglutide online

  28. LeroyAcuff表示:

    Профессиональный сервисный центр качественный ремонт телефонов ремонты телефонов

  29. Seven Reasons To Explain Why Door Fitting Birmingham Is Important local glaziers Birmingham

發佈留言

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