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,237,926 Responses

  1. 11 “Faux Pas” That Are Actually Acceptable To Create With Your Repair Upvc Window Upvc window repair

  2. Satta m表示:

    I’m not sure why but this web site is loading very
    slow for me. Is anyone else having this problem or is it a
    issue on my end? I’ll check back later on and see if the problem still exists.

  3. Darrelherse表示:

    yasal slot siteleri: deneme bonusu veren siteler – casino slot siteleri

  4. Darrelherse表示:

    slot bahis siteleri: slot siteleri 2024 – canl? slot siteleri

  5. Who Is Responsible For An Car Locksmiths Near Me Budget?
    12 Tips On How To Spend Your Money locksmith keys in Car

  6. xxx表示:

    homosexual porn

  7. Ten Upvc Window Repairs That Really Improve Your Life
    Upvc Window Repairs

  8. not safe表示:

    online slot

  9. 학생 대출表示:

    Compare Loans To Go In For An Advantage 학생 대출

  10. Hey! I’m at work surfing around your blog from my new apple iphone!
    Just wanted to say I love reading your blog
    and look forward to all your posts! Carry on the excellent work!

  11. Maurice表示:

    Does your blog have a contact page? I’m having a tough time
    locating it but, I’d like to send you an email.

    I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great website and I look forward to
    seeing it expand over time.

  12. Darrelherse表示:

    slot casino siteleri: guvenilir slot siteleri 2024 – slot bahis siteleri

  13. Joy表示:

    Guide To Mazda Key: The Intermediate Guide On Mazda Key mazda
    key (Joy)

  14. Darrelherse表示:

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

  15. You are so awesome! I don’t believe I’ve truly
    read a single thing like this before. So wonderful to find
    another person with original thoughts on this subject
    matter. Really.. many thanks for starting this up.

    This web site is one thing that is needed on the internet, someone with a bit of originality!

  16. lgbt porn表示:

    online slot

  17. I’m truly enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more enjoyable
    for me to come here and visit more often. Did you
    hire out a developer to create your theme? Fantastic work!

  18. Februar 2022 aktualisiert und ist kostenlos erhältlich.

  19. Guide To Skoda Replacement Key Near Me: The Intermediate Guide The
    Steps To Skoda Replacement Key Near Me skoda replacement Key near me

  20. Hi there it’s me, I am also visiting this website regularly, this web page
    is truly fastidious and the visitors are genuinely sharing
    nice thoughts.

  21. 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

  22. Replace表示:

    See What Replacement Jaguar Key Tricks The Celebs Are Making Use Of Replace

  23. Hello to evsry one,it’s genuinely a nice fⲟr me to pay a visit thiѕ site, it inclսdeѕ ᥙseful Ӏnformation.

    Review mу web page situs toto togel

  24. Darrelherse表示:

    en iyi slot siteler: deneme bonusu veren siteler – en iyi slot siteleri 2024

  25. цены на железобетонные изделия https://kupit-zhbi.ru/ .

  26. Darrelherse表示:

    en guvenilir slot siteleri: bonus veren slot siteleri – 2024 en iyi slot siteleri

  27. Darrelherse表示:

    slot siteleri 2024: slot siteleri 2024 – yasal slot siteleri

  28. KevinSounc表示:

    http://slotsiteleri.bid/# slot siteleri

  29. What’s up, the whole thing is going sound here and ofcourse every one is sharing data, that’s really
    excellent, keep up writing.

發佈留言

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