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,236,172 Responses

  1. ev taşıma表示:

    Evden Eve Nakliyat | Taşınma sırasında karşılaşılan zorlukları azaltmak için profesyonel bir nakliyat firmasının rolünü vurgulamanız çok yerinde. Kozcuoğlu Evden Eve Nakliyat olarak, müşterilerimize en iyi hizmeti sunmayı amaçlıyoruz.

  2. The other day, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a 40 foot drop, just so she can be
    a youtube sensation. My apple ipad is now broken and she
    has 83 views. I know this is totally off topic but I had
    to share it with someone!

  3. Pachinko offers an addictive mix of luck and strategy, making it a must-try for any gaming enthusiast.

  4. JetBlack表示:

    It’s iin fact very complex in this active life to listen news on Television, so I simply use web for thst purpose, and
    get the most recent information.

    Feel free to visit my webb blog … JetBlack

  5. KevinSounc表示:

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

  6. srasurvivors表示:

    What’s up, always i used to check web site posts here early
    in the daylight, since i enjoy to gain knowledge of more and more.

  7. I was wondering if you ever considered changing the structure of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.

    Youve got an awful lot of text for only having 1 or two images.
    Maybe you could space it out better?

  8. Florene表示:

    I could not resist commenting. Well written!

  9. Ev Taşıma | Taşınma sürecinde dikkat edilmesi gereken noktaları bu kadar detaylı açıklamanız çok faydalı olmuş. Kozcuoğlu Ev Taşıma olarak, müşterilerimize bu konuda rehberlik etmekten memnuniyet duyuyoruz.

  10. 7 Things About Glass Doctor Near Me You’ll Kick Yourself For Not Knowing historic window Repair

  11. Lucio表示:

    You really make it appear really easy together
    with your presentation however I in finding this topic to be really one thing which I think I’d never understand.
    It sort of feels too complicated and very large for me.

    I’m looking ahead in your subsequent post, I will try to get the dangle of it!

  12. bokep terbaru表示:

    Hello! Someone in my Facebook group shared this website with us so I came to give it a look.
    I’m definitely loving the information. I’m book-marking and will be tweeting this to
    my followers! Wonderful blog and excellent style and design.

  13. HerbertemuRl表示:

    hi guys i articulate that https://wplgsturapwmestv185.ru/

  14. incest porn表示:

    hacklink

  15. This is really interesting, You are a very skilled blogger.
    I have joined your feed and look forward to seeking more of your magnificent post.
    Also, I have shared your web site in my social networks!

  16. KevinSounc表示:

    http://sweetbonanza.network/# slot oyunlari

  17. Jens表示:

    Asking questions are in fact fastidious thing if
    you are not understanding anything entirely, but this piece
    of writing presents fastidious understanding even.

  18. porn cannibalism

  19. Darrelherse表示:

    en iyi slot siteleri 2024: slot bahis siteleri – en iyi slot siteler

  20. Ten Myths About Patio Door Repair That Aren’t Always The Truth tilt and slide patio Door repairs Near me

  21. plant food表示:

    I’m not sure why but this blog is loading very slow for me.
    Is anyone else having this issue or is it a issue on my end?

    I’ll check back later on and see if the problem
    still exists.

  22. y8 games表示:

    Hey would you mind sharing which blog platform you’re working with?
    I’m going to start my own blog in the near future but I’m having a tough time
    selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs
    and I’m looking for something completely unique.
    P.S Apologies for being off-topic but I had to ask!

發佈留言

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