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

  1. You’ve made your point very effectively!.

  2. lost表示:

    20 Fun Facts About Bentley Key Price lost

  3. bandar Toto表示:

    7 Easy Tips For Totally Rocking Your Situs Toto Togel bandar Toto

  4. Definitely believe that which you stated. Your favorite justification seemed to be on the net the easiest thing
    to be aware of. I say to you, I definitely get annoyed while people consider worries that they just
    don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect ,
    people could take a signal. Will likely be back to get more.
    Thanks

  5. Just want to say your article is as surprising. The clearness for your put up is
    just great and that i could suppose you’re knowledgeable in this subject.
    Well with your permission let me to seize your feed to stay up
    to date with forthcoming post. Thank you one million and please continue the rewarding work.

  6. Amazing blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost
    on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There
    are so many choices out there that I’m totally confused ..
    Any recommendations? Appreciate it!

    Also visit my webpage … thumb latch trigger

  7. KevinSounc表示:

    https://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  8. tire changer表示:

    I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get set up?
    I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% sure. Any tips or advice would be greatly appreciated.
    Thanks

  9. The 15 Things Your Boss Would Like You To Know
    You Knew About Citroen Berlingo Van Key Replacement key Cutting service

  10. в одних вариантах игры помимо этого – используется расклад Фор
    флаш («сам-четвёрт») – четыре карты одной масти и одна карта другой
    масти.

    Visit my web page; https://aiheconglinkb.com/

  11. наркология вывод из запоя в стационаре https://vyvod-iz-zapoya-sochi12.ru .

  12. KevinSounc表示:

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

  13. You’ll Be Unable To Guess Mini Cooper Car Key Replacement’s Benefits Mini Cooper Car Key Replacement

  14. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza demo

  15. Five Killer Quora Answers On Repair Bifold Door Top Roller Repair Bifold Door Top Roller

  16. zwoltek01表示:

    Every weekend i used to go to see this site, for the reason that i want enjoyment, since this this site conations in fact
    fastidious funny information too.

    my web blog :: zwoltek01

  17. What’s The Job Market For Daftar Situs Togel Professionals?
    daftar situs togel

  18. What’s The Current Job Market For Leather Sectional Sleeper Sofa Professionals?

    leather sectional sleeper sofa

  19. A Delightful Rant About What Is Anxiety And Symptoms Male Depression And Anxiety Symptoms

  20. Guide To Coffee Beans Uk: The Intermediate Guide In Coffee Beans Uk coffee beans uk

  21. Have a look at my blog; Daycare Near Me

  22. This Week’s Top Stories Concerning Inattentive ADHD Medication Medication For Odd And Adhd

  23. 9 Lessons Your Parents Teach You About Freestanding Electric Stove Fire freestanding Electric stove Fire

  24. spare表示:

    You’ll Be Unable To Guess Bentley Keys’s Secrets spare

  25. Alycia表示:

    Aw, this was a very good post. Taking the time and actual effort to make a great article… but what can I say… I hesitate a whole lot and don’t
    manage to get nearly anything done.

  26. 20 Tips To Help You Be Better At Skoda Car Key
    skoda replacement key cost

  27. bunk bed usa表示:

    A Look At The Ugly Facts About Best Double
    Bunk Beds bunk bed usa

發佈留言

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