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,230,558 Responses

  1. Howdy! This blog post couldn’t be written much better!
    Going through this post reminds me of my previous roommate!
    He continually kept preaching about this. I will forward this article
    to him. Pretty sure he’ll have a very good read.
    Thanks for sharing!

  2. Greetings! Very useful advice in this particular article!
    It’s the little changes that will make the
    most important changes. Thanks for sharing!

  3. Rickysem表示:

    ordering drugs from canada canadian discount pharmacy reputable canadian pharmacy

  4. What’s The Reason Everyone Is Talking About Uk Online Shopping Sites For Mobile Right Now Adjustable Swivel Chair

  5. Присоединиться может любой игрок в удобное для него время.

  6. 15 Things You’re Not Sure Of About Window Seal Replacement window replacement companies

  7. 10 Factors To Know Concerning Mesothelioma
    Lawsuits You Didn’t Learn At School mesothelioma Claim

  8. Pedroereva表示:

    mexican rx online: cheapest mexico drugs – medicine in mexico pharmacies

  9. ck4d login表示:

    Ԍreat article! Thhat iіs the type of info that are meant to be shared
    aϲrоss the internet. Shame on the searcһ engines
    foг now not ρositioning this submit upper! Come on over and ttalk over wwith my
    web ѕite . Thank you =)

    Here is my website :: ck4d login

  10. Stevenrip表示:

    canada drug pharmacy: Certified Canadian Pharmacies – legit canadian pharmacy online

  11. Stevenrip表示:

    canada pharmacy online legit: canadian pharmacy prices – canadian drug pharmacy

  12. Pedroereva表示:

    mexico pharmacy: cheapest mexico drugs – purple pharmacy mexico price list

  13. Marcelabelp表示:

    https://indiaph24.store/# pharmacy website india

  14. Marcelabelp表示:

    https://indiaph24.store/# Online medicine home delivery

  15. 5 Things Everyone Gets Wrong About Emergency Car Locksmith
    car locksmith near me

  16. The Best Automatic Folding Mobility Scooters Tricks For Changing Your Life best Automatic folding mobility scooter

  17. 5 People You Should Be Getting To Know In The Designer Handbags And Purses Industry Online Designer Handbags

  18. A Look Inside Double Glazing Repair Luton’s Secrets Of Double Glazing Repair Luton window Repair near me

  19. Marcelabelp表示:

    https://mexicoph24.life/# mexico drug stores pharmacies

  20. How Nissan Qashqai Key Replacement Has Changed My
    Life The Better Nissan juke Spare key

  21. 10 Times You’ll Have To Learn About Avon Book avon new book (0522445518.ussoft.kr)

  22. Rickysem表示:

    mexican rx online Online Pharmacies in Mexico medication from mexico pharmacy

  23. A Proficient Rant About Black Fridge Freezer
    best 70 30 fridge freezer – yogaasanas.science,

  24. Marcelabelp表示:

    https://indiaph24.store/# best online pharmacy india

  25. Uta表示:

    How To Find Out If You’re Are Ready For
    Become A Avon Representative become a avon representative – Uta,

  26. Could Misted Double Glazing Repairs Near Me Be
    The Key To Dealing With 2023? double glazing door [test.gitaransk.ru]

  27. I was recommended this blog by my cousin. I’m not
    sure whether this post is written by him as no one else know
    such detailed about my trouble. You’re amazing!

    Thanks!

  28. Don’t Buy Into These “Trends” Concerning Car Door Key Lock
    Repair car central locking system repair near me

  29. JustinCit表示:

    top 10 online pharmacy in india http://indiaph24.store/# buy prescription drugs from india
    india pharmacy mail order

發佈留言

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