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,229,021 Responses

  1. Hello, I think your blog might be having browser compatibility issues.

    When I look at your website in Ie, it looks fine but when opening in Internet Explorer, it has
    some overlapping. I just wanted to give you a quick heads up!
    Other then that, awesome blog!

  2. Diplomi_sfMl表示:

    купить диплом ржд many-diplom77.ru .

  3. HassanbuifS表示:

    https://denemebonusuverensiteler.top/# deneme bonusu veren siteler mycbet.com
    denemebonusuverensiteler.top

  4. Great article! This is the kind of info that should be shared around the net.
    Shame on the seek engines for not positioning this submit upper!
    Come on over and seek advice from my site . Thank
    you =)

    https://ww5.datasydney6d.co/

  5. xxx表示:

    This is a good tip particularly to those fresh to
    the blogosphere. Simple but very precise information… Many thanks for sharing this one.
    A must read article!

  6. Great post! We will be linking to this particularly great article on our site.
    Keep up the good writing.

  7. I know this web page provides quality based articles
    and extra stuff, is there any other web page which presents such things in quality?

    https://ww1.angkanetpro.buzz/

  8. When I initially commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I get four emails
    with the same comment. Is there any way you can remove me from that service?

    Thanks!

  9. Diplomi_qjMl表示:

    где купить аттестат за 9 класс где купить аттестат за 9 класс .

  10. Hello! I’m at work surfing around your blog from
    my new iphone! Just wanted to say I love reading your blog and look forward to all
    your posts! Carry on the fantastic work!

  11. Good answers in return of this issue with real arguments and explaining everything concerning that.

  12. It’s hard to come by educated people about this subject, but you sound like you know what you’re talking about!
    Thanks

  13. cor duties表示:

    Spot on with this write-up, I honestly believe this amazing site
    needs much more attention. I’ll probably be returning to read through more, thanks for the advice!

  14. LarryCoell表示:

    denemebonusuverensiteler.top deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler

  15. Vera表示:

    What’s The Current Job Market For Asbestos Mesothelioma Professionals Like?
    mesothelioma lawyers (Vera)

  16. Diplomi_xmkn表示:

    купить диплом недорого нижний новгород 1russa-diploms.ru .

  17. Guide To Best Home Coffee Machine: The Intermediate Guide
    Towards Best Home Coffee Machine Best Home Coffee Machine

  18. rutelochki.ru表示:

    15 Funny People Working Secretly In Vauxhall Keys new key for vauxhall
    vivaro Van; rutelochki.ru,

  19. HassanbuifS表示:

    https://casinositeleri.win/# Canl? Casino Siteleri
    deneme bonusu veren siteler betturkey

  20. Live Sydney表示:

    When I originally commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I get several emails with the same
    comment. Is there any way you can remove me from that service?
    Many thanks!

    https://ww9.mbahtogel.top/

  21. 9 Things Your Parents Taught You About Bifold Repairs Near Me bifold repairs near me

  22. Derekfar表示:

    en cok kazand?ran slot oyunlar?: slot tr online – slot tr online

發佈留言

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