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,367,510 Responses

  1. Stephengen表示:

    http://vgrsansordonnance.com/# SildГ©nafil Teva 100 mg acheter

  2. Профессиональный сервисный центр по ремонту принтеров в Москве.
    Мы предлагаем: срочный ремонт принтера
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  3. HeathZex表示:

    Pharmacie sans ordonnance: Cialis generique prix – pharmacie en ligne fiable

  4. Stephengen表示:

    https://vgrsansordonnance.com/# Viagra en france livraison rapide

  5. metro banners表示:

    Wow, fantastic weblog layout! How lengthy have you been running
    a blog for? you made blogging glance easy.

    The full glance of your website is great, let alone the
    content!

  6. HeathZex表示:

    Prix du Viagra en pharmacie en France: Acheter du Viagra sans ordonnance – Viagra sans ordonnance livraison 48h

  7. Sweet Bonanza Multipliers – Boost Your Wins! Multiply your winnings with Sweet Bonanza’s unique multiplier feature. Learn how to activate and maximize these powerful bonuses during your spins.
    slot bonanza

  8. 5 Common Myths About Non Prescription ADHD Medication Uk You Should Avoid adhd medication titration

  9. Stephengen表示:

    https://clssansordonnance.icu/# pharmacie en ligne fiable

  10. Stephengen表示:

    https://vgrsansordonnance.com/# Viagra sans ordonnance livraison 48h

  11. Hey There. I found your blog using msn. That is a really neatly written article.

    I’ll make sure to bookmark it and return to learn more of your helpful info.
    Thanks for the post. I’ll certainly return.

  12. JamesBuice表示:

    mexico drug stores pharmacies: mexican pharma – mexican drugstore online
    purple pharmacy mexico price list

  13. HeathZex表示:

    pharmacie en ligne fiable: pharmacie en ligne france fiable – pharmacie en ligne sans ordonnance

  14. Charlesrab表示:

    Viagra sans ordonnance livraison 24h viagra en ligne Viagra vente libre pays

  15. 20 Things You Should Be Asking About Daftar Akun Togel Resmi
    Before You Buy Daftar Akun Togel Resmi Togel Singapore

  16. Stephengen表示:

    https://clssansordonnance.icu/# pharmacie en ligne france fiable

  17. Stephengen表示:

    http://clssansordonnance.icu/# pharmacie en ligne pas cher

  18. HeathZex表示:

    Acheter Sildenafil 100mg sans ordonnance: Viagra generique en pharmacie – Viagra 100mg prix

  19. Cazrhgg表示:

    Привет, друзья!
    Мы готовы предложить документы ВУЗов
    alik.forumrpg.ru/viewtopic.php?id=7642#p281755

  20. Hi there! This blog post couldn’t be written much better!
    Reading through this article reminds me of my previous roommate!
    He continually kept preaching about this. I will send this information to him.
    Pretty sure he will have a good read. I
    appreciate you for sharing!

  21. Профессиональный сервисный центр по ремонту объективов в Москве.
    Мы предлагаем: ремонт диафрагмы объектива
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  22. Charlesrab表示:

    pharmacie en ligne Medicaments en ligne livres en 24h Achat mГ©dicament en ligne fiable

  23. JamesBuice表示:

    pharmacies in mexico that ship to usa: mexican drugstore online – medication from mexico pharmacy
    medicine in mexico pharmacies

  24. 15 Things You Didn’t Know About Mobility Scooters lightweight Foldable scooters

  25. I always used to study paragraph in news papers but now
    as I am a user of internet so from now I am using net for content, thanks to web.

  26. Stephengen表示:

    http://pharmaciepascher.pro/# Pharmacie Internationale en ligne

  27. Realistic Sexdoll Tools To Streamline Your Everyday Lifethe Only Realistic Sexdoll Trick
    Every Individual Should Know realistic Sexdolls (sex-toys-for-adults00679.theisblog.com)

  28. Why All The Fuss? Link Togel ? Link togel resmi

  29. This is a topic which is close to my heart… Take care!
    Exactly where are your contact details though?

  30. Stephengen表示:

    https://vgrsansordonnance.com/# SildГ©nafil 100mg pharmacie en ligne

發佈留言

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