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,232,897 Responses

  1. Hi! This is kind of off topic but I need some advice from an established blog.
    Is it very hard to set up your own blog? I’m not
    very techincal but I can figure things out pretty quick.
    I’m thinking about setting up my own but
    I’m not sure where to begin. Do you have any tips or suggestions?
    Many thanks

  2. mega888表示:

    Its not my first time to go to see this web page, i am visiting
    this website dailly and obtain fastidious data from here daily.

  3. unfriendcoal.com la mot trang web livetream truc tiep bong
    da gia mao thuong hieu cakhiatv cua cac binh luan vien quen thuoc nhu giang a lu, giang a pho, giang a cay…Ngoai ra trang
    unfriendcoal.com con thuong xuyen gioi thieu cac trang web ca do, keo nha cai, lua dao nguoi xem.
    Noi chung unfriendcoal.com la mot website lua dao, khong uy tin anh
    huong nghiem trong den cong dong nguoi yeu thich bong da, mon the thao vua

  4. ev taşıma表示:

    Ev Taşıma | Müşteri memnuniyetinin ev taşıma sektöründe ne kadar önemli olduğunu bu yazıda çok iyi ifade etmişsiniz. Kozcuoğlu Ev Taşıma olarak, memnuniyeti her zaman ön planda tutuyoruz.

  5. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza 90 tl

  6. Meds information sheet. Brand names.
    how can i get ramipril pills
    Actual news about medicament. Read information now.

  7. What’s a good wordpress blog theme for an icanhascheeseburer or failblog clone?

  8. Tony Callow表示:

    Would becoming a paid blogger be a good idea to get money?

  9. KevinSounc表示:

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

  10. Darrelherse表示:

    guvenilir slot siteleri: slot siteleri guvenilir – guvenilir slot siteleri

  11. KevinSounc表示:

    http://slotsiteleri.bid/# deneme bonusu veren siteler

  12. ev taşıma表示:

    Evden Eve Nakliyat | Taşınma sırasında doğru planlamanın önemini anlatan bu makale, müşterilerimize rehberlik edecek nitelikte. Kozcuoğlu Evden Eve Nakliyat olarak, taşınma sürecini her zaman en iyi şekilde yönetiyoruz.

  13. Karolin表示:

    Good day! I know this is kinda off topic but I was wondering
    if you knew where I could locate a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  14. Hey this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding experience so I wanted to
    get guidance from someone with experience. Any help would be enormously appreciated!

    Feel free to surf to my webpage: Amazon EC2 AMI

  15. raja bandot表示:

    Generally I do not read article on blogs, however I wish to say that
    this write-up very compelled me to try and do so!
    Your writing taste has been amazed me. Thanks,
    quite great post.

  16. If some one wants expert view on the topic of blogging then i propose him/her to go to
    see this weblog, Keep up the good work.

    Also visit my web site :: EC2 Image Builder

  17. Darrelherse表示:

    guvenilir slot siteleri 2024: slot siteleri guvenilir – slot siteleri guvenilir

  18. Rosaria表示:

    I got this website from my buddy who shared with me about
    this web site and now this time I am browsing this website and
    reading very informative articles here.

  19. porn表示:

    lgbt porn

  20. However, simply implementing a strategy won’t suffice; you have to stick to it till the very end.

  21. Heya i am for the first time here. I found this board and I find It truly helpful &
    it helped me out a lot. I hope to present one thing back and aid
    others such as you helped me.

  22. Hurrah! At last I got a webpage from where I can genuinely get helpful
    data regarding my study and knowledge.

  23. Sextreffen表示:

    Great goods from you, man. I’ve understand your stuff previous to and you
    are just extremely wonderful. I actually like what you have acquired here,
    certainly like what you are saying and the way in which you
    say it. You make it entertaining and you still take care of to keep it sensible.

    I cant wait to read far more from you. This is actually a tremendous web site.

    Feel free to surf to my homepage :: Sextreffen

  24. Darrelherse表示:

    casino slot siteleri: en iyi slot siteler – oyun siteleri slot

  25. You really make it seem really easy along with your
    presentation however I to find this topic to be really something that I think I
    might by no means understand. It seems too complicated and extremely huge for me.
    I’m having a look ahead on your next put up,
    I will attempt to get the hold of it!

  26. KevinSounc表示:

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

  27. casino表示:

    homosexual porn

發佈留言

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