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,518,078 Responses

  1. Darrelherse表示:

    en yeni slot siteleri: deneme bonusu veren siteler – oyun siteleri slot

  2. The 10 Scariest Things About Retro Fridge Freezer Sale retro
    fridge freezer sale – https://tongbet24.com,

  3. Thanks for a marvelous posting! I really enjoyed reading it, you could be a great author.I will
    be sure to bookmark your blog and may come back from now on. I want
    to encourage you continue your great writing, have a nice
    morning!

  4. click here表示:

    Good way of explaining, and good post to take facts on the topic of my presentation topic,
    which i am going to deliver in academy.

  5. Adriana表示:

    You’ll Never Be Able To Figure Out This Is Treadmill
    Incline Good’s Benefits is treadmill incline good (Adriana)

  6. 10 Things You Learned In Preschool That Can Help You In Twin Stroller With Car Seats double Pram buggy

  7. KevinSounc表示:

    http://slotsiteleri.bid/# slot kumar siteleri

  8. WIFI TEST表示:

    Magnificent site. A lot of useful info here. I’m sending it to some friends ans also sharing in delicious.
    And of course, thank you in your sweat!

  9. 5 Killer Quora Answers To Bifold Door Glass Replacement Cost Bifold Door Glass Replacement Cost

  10. Darrelherse表示:

    bonus veren slot siteleri: slot oyunlar? siteleri – yeni slot siteleri

  11. winzada表示:

    Having read this I thought it was extremely enlightening.
    I appreciate you finding the time and energy to put this article together.

    I once again find myself spending a significant amount of time both reading and commenting.
    But so what, it was still worthwhile!

  12. It’s The One Single Beds Mattresses Trick Every Person Should Be Aware Of Mattresses Single Bed

  13. 5 Killer Quora Answers On Small Double Buggy Small Double Buggy

  14. Darrelherse表示:

    2024 en iyi slot siteleri: canl? slot siteleri – oyun siteleri slot

  15. toket brutal表示:

    I’m curious to find out what blog system you have been using?
    I’m experiencing some minor security problems with
    my latest site and I would like to find something more safe.
    Do you have any recommendations?

  16. The Best Retro Fridge Freezer Black Tricks To Make A Difference
    In Your Life best retro Fridge freezer

  17. Sage Advice About Treadmill Incline From A Five-Year-Old
    Small space treadmill with incline

  18. click here表示:

    If you wish for to obtain a great deal from this post then you have to apply such methods to your
    won weblog.

  19. KevinSounc表示:

    https://denemebonusuverensiteler.win/# bonus veren siteler

  20. فلزیاب表示:

    طلایاب

  21. You’re so interesting! I do not believe I’ve read a single thing like
    that before. So good to discover another person with a few unique thoughts on this subject.
    Seriously.. thank you for starting this up. This web site is something that’s needed on the web, someone with
    a bit of originality!

    Feel free to surf to my site: where can i buy tonic greens

  22. It’s an amazing article for all the online users; they will
    take benefit from it I am sure.

  23. Situs 4D表示:

    Guide To Situs 4d: The Intermediate Guide The Steps
    To Situs 4d Situs 4D

  24. 10 Things You’ll Need To Know About Depression Symptoms And Anxiety Early Depression symptoms

  25. 10 Car Key Lamborghini-Related Projects To Stretch Your Creativity lamborghini smart key

  26. The Electric Motorized Treadmill Case Study You’ll Never Forget motorized treadmills

  27. 14 Businesses Doing A Superb Job At Adult ADHD
    In Women mild adhd In women – http://Hongseongilbo.Kr/,

  28. 15 Terms That Everyone Working In The Citroen Key Replacement Cost Industry Should
    Know Replacement Citroen Key

  29. Darrelherse表示:

    yasal slot siteleri: canl? slot siteleri – casino slot siteleri

發佈留言

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