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,230,049 Responses

  1. Darrelherse表示:

    slot siteleri guvenilir: slot siteleri 2024 – en cok kazandiran slot siteleri

  2. Darrelherse表示:

    en yeni slot siteleri: deneme veren slot siteleri – slot siteleri 2024

  3. Xazrmfb表示:

    Привет, друзья!
    Заказать документ о получении высшего образования.
    leydis16.phorum.pl/posting.php?mode=newtopic&f=1

  4. Системно-семейные расстановки https://rasstanovkiural.ru

  5. Darrelherse表示:

    casino slot siteleri: yasal slot siteleri – slot casino siteleri

  6. all the time i used to read smaller articles which also clear their
    motive, and that is also happening with this piece of writing which
    I am reading at this place.

  7. Darrelherse表示:

    yasal slot siteleri: slot siteleri guvenilir – bonus veren slot siteleri

  8. My spouse and I stumbled over here from a different web page and
    thought I might as well check things out. I like what I see so now i am following you.
    Look forward to looking over your web page for a second time.

  9. It is perfect time to make a few plans for the future and it’s time to be
    happy. I’ve read this publish and if I could I desire to recommend you some interesting
    things or tips. Perhaps you could write subsequent articles referring to this article.
    I wish to learn even more things approximately it!

    Stop by my web-site – dentavim chews

  10. ev taşıma表示:

    Evden Eve Nakliyat | Makalede yer alan taşınma önerilerini çok beğendik. Kozcuoğlu Evden Eve Nakliyat olarak, bu süreçte müşterilerimizin yanında olmaktan mutluluk duyuyoruz.

  11. Replacement表示:

    20 Key Fob Toyota Websites Taking The Internet
    By Storm Replacement

  12. Hi my loved one! I wish to say that this post is awesome, great written and include approximately all important
    infos. I’d like to peer more posts like this .

  13. This piece of writing presents clear idea designed for
    the new users of blogging, that truly how to do blogging.

  14. In recent years, the popularity of CBD products has skyrocketed, and with the increased interest comes a
    plethora of reviews and testimonials from users.
    Canna Plus CBD, a prominent brand in the market, has garnered significant attention.

  15. Remarkable! Its in fact amazing piece of writing, I have got much clear idea regarding from this piece of writing.

  16. This information is priceless. How can I find out
    more?

  17. Persons waiting for the arrival of passengers can park
    free of charge in the short-term or long-term parking for the first 30 minutes
    or wait free of charge in our long-how much is long-term parking at dulles airport for
    cellular smartphones.

  18. click now表示:

    I know this if off topic but I’m looking into starting my own blog and
    was curious what all is required to get set up?
    I’m assuming having a blog like yours would cost
    a pretty penny? I’m not very internet savvy so I’m not 100% positive.
    Any tips or advice would be greatly appreciated. Many
    thanks

  19. KevinSounc表示:

    http://slotsiteleri.bid/# slot oyunlar? siteleri

  20. HELLO – HI — With the support of two decades has taken a big step in solving medical – https://bornlady.ir/hair-color/ice-caramel-hair-color-without-bleaching/

  21. Greetings from Idaho! I’m bored to death at work so I decided to check
    out your site on my iphone during lunch break. I enjoy the information you
    provide here and can’t wait to take a look when I
    get home. I’m surprised at how quick your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyhow, fantastic site!

  22. KevinSounc表示:

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

  23. see here now表示:

    Its not my first time to go to see this web
    site, i am browsing this website dailly and take nice information from here everyday.

  24. Medicines information leaflet. Cautions.
    cost generic arimidex tablets
    All about drugs. Read information here.

  25. KevinSounc表示:

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

  26. discover this表示:

    I’m not that much of a online reader to be honest but your sites really nice, keep it
    up! I’ll go ahead and bookmark your website to come back down the road.
    Many thanks

  27. thịt vịt表示:

    What’s up everyone, it’s my first pay a quick visit at this website, and paragraph is
    actually fruitful in favor of me, keep up posting
    these types of posts.

發佈留言

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