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,234,016 Responses

  1. Randallabevy表示:

    vavada casino регистрация https://slothacker62.com

  2. KevinSounc表示:

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

  3. Ahaa, its good dialogue regarding this paragraph at this
    place at this blog, I have read all that, so at
    this time me also commenting here.

  4. Darrelherse表示:

    yeni slot siteleri: deneme veren slot siteleri – en iyi slot siteler

  5. Kennethmab表示:

    Официальный клуб Sukaaa предлагает новым игрокам бонус 325% за регистрацию, за активное повышение аккаунта – игрок получает кэшбэк и быстрые выводы. Новых клиентов привлекает не только бездепозитный бонус за регистрацию, но и дизайн https://t.me/s/sykaaa_official_casino на сайте наглядное меню, которое позволяет в пару кликов найти нужный софт, выбрать слот или сделать депозит.

  6. Darrelherse表示:

    oyun siteleri slot: yasal slot siteleri – en cok kazandiran slot siteleri

  7. Hello would you mind sharing which blog platform you’re using?
    I’m planning to start my own blog in the near future but I’m having a
    difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and
    I’m looking for something unique. P.S Apologies for being off-topic but I had to ask!

  8. Darrelherse表示:

    oyun siteleri slot: oyun siteleri slot – en iyi slot siteleri 2024

  9. Thomas Cook Germany introduced that it might close down on 1 December 2019, after having failed to find a purchaser, with administrators in talks to sell Thomas Cook Germany’s tour operators Öger Tours, Bucher Reisen, Neckermann and Air Marin and discover buyers for resort manufacturers Sentido and Smartline.

  10. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You clearly know what youre talking about,
    why throw away your intelligence on just posting videos to your weblog when you could be giving
    us something enlightening to read?

  11. What’s up i am kavin, its my first time to commenting anyplace, when i read
    this piece of writing i thought i could also make comment due to this
    brilliant post.

  12. Hi, I wish for to subscribe for this web site to obtain most up-to-date updates, so where can i do it please assist.

  13. Pretty section of content. I just stumbled upon your site and in accession capital to assert that I
    get actually enjoyed account your blog posts.
    Anyway I will be subscribing to your feeds and even I achievement you
    access consistently quickly.

  14. Darrelherse表示:

    slot kumar siteleri: yasal slot siteleri – yasal slot siteleri

  15. Medicine prescribing information. Brand names.
    how can i get pulmicort price
    Everything information about medicament. Read information here.

  16. Darrelherse表示:

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

  17. Darrelherse表示:

    deneme veren slot siteleri: bonus veren slot siteleri – en guvenilir slot siteleri

  18. Darrelherse表示:

    oyun siteleri slot: en iyi slot siteleri – en guvenilir slot siteleri

  19. Darrelherse表示:

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

  20. Heriberto表示:

    I’m not sure why but this site is loading incredibly slow for me.

    Is anyone else having this issue or is it a problem on my end?
    I’ll check back later on and see if the problem still exists.

  21. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza free spin demo

  22. Source表示:

    Why visitors still use to read news papers when in this technological globe the whole
    thing is existing on web?

  23. Ev Taşıma | Taşınma sürecinde karşılaşılan zorluklara değinmeniz çok yerinde olmuş. Biz de Kozcuoğlu Ev Taşıma olarak, müşterilerimizin bu zorlukları minimuma indirmek için en iyi çözümleri sunuyoruz.

  24. A片表示:

    I like reading an article that will make men and women think.
    Also, thank you for allowing me to comment!

    My blog post: A片

  25. Darrelherse表示:

    deneme bonusu veren siteler: bonus veren slot siteleri – slot kumar siteleri

發佈留言

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