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,228,843 Responses

  1. Системно-семейные расстановки по Хеллингеру. https://rasstanovkiural.ru

  2. Darrelherse表示:

    guvenilir slot siteleri 2024: bonus veren casino slot siteleri – slot siteleri

  3. KevinSounc表示:

    https://slotsiteleri.bid/# casino slot siteleri

  4. Darrelherse表示:

    slot oyunlar? siteleri: slot siteleri guvenilir – canl? slot siteleri

  5. It’s the best time to make a few plans for the long run and it is
    time to be happy. I’ve learn this publish and if I may I desire to counsel you few attention-grabbing issues
    or tips. Maybe you could write next articles relating to
    this article. I wish to learn even more things about it!

    my web blog :: USA Script Helpers

  6. KevinSounc表示:

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

  7. My partner and I absolutely love your blog and find
    a lot of your post’s to be exactly what I’m looking for.

    can you offer guest writers to write content in your case?
    I wouldn’t mind publishing a post or elaborating on a lot of
    the subjects you write concerning here. Again, awesome web site!

  8. When some one searches for his required thing, thus he/she wants to be available that in detail, therefore that thing is maintained over here.

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

  10. Hi there mates, pleasant paragraph and fastidious arguments commented at this place, I am truly enjoying by these.

  11. Darrelherse表示:

    en guvenilir slot siteleri: slot casino siteleri – 2024 en iyi slot siteleri

  12. Drugs information sheet. Generic Name.
    where can i buy cheap compazine for sale
    Some trends of medicine. Read information here.

  13. Darrelherse表示:

    slot kumar siteleri: deneme bonusu veren slot siteleri – slot siteleri 2024

  14. Automaty do counter strike: global offensive – pokazy, dostępne promocje.
    Badania demonstrują, że zachęca własnych graczy do ustalania przeciwwskazań
    i paysafecard, https://experiment.com/users/Forpart z Polski.

  15. 1表示:

    Εverything is very open ѡith a precise description of the issues.
    It was ɗefіnitely infⲟrmatіve. Your site is
    useful. Thanks for sharing!

    Here is my site :: 1

  16. 11 Super Steps Giving The Perfect Massage Along With A Massage Stone 대덕구오피 (http://fitnessthroughdance.com/)

  17. onlyfans表示:

    Why users still use to read news papers when in this technological globe all is accessible on web?

  18. slots表示:

    Hmm it appears like your website ate my first comment
    (it was super long) so I guess I’ll just sum it up what I
    had written and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I’m still new to everything.
    Do you have any points for inexperienced blog writers? I’d
    really appreciate it.

  19. Darrelherse表示:

    2024 en iyi slot siteleri: en iyi slot siteleri 2024 – slot oyunlar? siteleri

  20. This is a topic that is near to my heart… Thank you!
    Exactly where are your contact details though?

    my blog – pronerve 6 reviews

  21. KevinSounc表示:

    http://slotsiteleri.bid/# slot siteleri

  22. Darrelherse表示:

    slot kumar siteleri: en iyi slot siteleri – casino slot siteleri

  23. KevinSounc表示:

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

  24. lawinplay表示:

    Lawinplay – You can Use GCash to play in anytime
    Free Spins 1000+ Free Demo Slot
    Openning Bonus 120% piso back
    Searching lawinplay888 on Google or Bing to get free coins

    https://lawinplay888.com

發佈留言

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