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,375,261 Responses

  1. Frankevild表示:

    https://ozempic.art/# ozempic

  2. I know this web site provides quality dependent content and additional information, is there any other site which presents these
    information in quality?

  3. В магазине сейфов предлагают сейфы купить в москве сейфы москва

  4. The Reasons Folding Transport Wheelchair In 2023 Is The Main Focus Of All People’s Attention.
    2023 wheelchair lightweight folding

  5. Why Assessment For Mental Health Is Your Next Big Obsession mental status assessment,Daniele,

  6. transsexual表示:

    I’m curious to find out what blog system you’re utilizing?
    I’m having some minor security problems with my latest
    website and I’d like to find something more secure.
    Do you have any solutions?

  7. Michale表示:

    Hello there! Do you know if they make any plugins to assist with SEO?
    I’m trying to get my blog to rank for some targeted
    keywords but I’m not seeing very good success. If you know of any please share.
    Thank you!

  8. Jayden Hines表示:

    15 Gifts For Your Mental Assessment Lover In Your Life psychiatrist Mental health assessment

  9. RamonRek表示:

    cheapest rybelsus pills: semaglutide online – rybelsus price

  10. В магазине сейфов предлагают сейф цена купить сейф цена москва

  11. Bet Casino表示:

    Cheers! A good amount of facts!

    Here is my website; https://mostbet-bk.pl

  12. Maricruz表示:

    See What Demist Double Glazing Near Me Tricks The Celebs Are
    Utilizing demist double glazing – Maricruz,

  13. Thanks for sharing your info. I truly appreciate your efforts and I will be waiting for your next post thanks once again.

  14. Where Can You Find The Top Treadmills With Incline Information? Small treadmill incline

  15. RamonRek表示:

    cheapest rybelsus pills: semaglutide online – rybelsus pill

  16. вывод из запоя круглосуточно ростов вывод из запоя круглосуточно ростов .

  17. Hi there, I found your site by the use of Google while searching
    for a comparable matter, your site came up, it
    appears to be like great. I have bookmarked it in my google bookmarks.

    Hello there, just turned into alert to your weblog thru Google, and found that it is truly informative.
    I’m gonna watch out for brussels. I will appreciate when you continue this in future.

    A lot of folks might be benefited from your writing.
    Cheers!

  18. Frankevild表示:

    https://rybelsus.shop/# rybelsus pill

  19. The 9 Things Your Parents Taught You About Comfortable Couches For Sale Comfortable Couches For Sale

  20. RamonRek表示:

    ozempic coupon: ozempic cost – buy ozempic

  21. How To Save Money On Sectional Couches Sectional couches with Recliners

  22. Профессиональный сервисный центр по ремонту электросамокатов в Москве.
    Мы предлагаем: тюнинг электросамокатов в москве
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  23. How To Outsmart Your Boss On Upvc Door Lock Replacement replacing a Upvc door panel

  24. Jeremypag表示:

    cheapest rybelsus pills semaglutide tablets rybelsus coupon

  25. The 10 Scariest Things About Window Glass Replacement window Glass Replacement

  26. Miriam表示:

    5 Clarifications On Electric Fires Wall Mounted electric fireplace hang on wall, Miriam,

  27. BarryDof表示:

    http://rybelsus.shop/# semaglutide tablets

  28. singapore表示:

    It’s really a great and useful piece of info. I am glad that
    you shared this useful information with us. Please keep us up to date like
    this. Thank you for sharing.

發佈留言

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