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,467,309 Responses

  1. Michaelcib表示:

    Portugal has long been a country known for producing exceptional talent in the world of sports. With a rich history in football, many athletes from this nation have achieved international fame through their incredible skills and accomplishments http://letmefix.lt/index.php?title=_The_Environmental_Impact_of_Training_LGPT_Models

  2. bluetoothhit表示:

    I know this website gives quality depending posts and
    additional information, is there any other site which offers these stuff in quality?

  3. Unexpected Business Strategies That Helped Upvc Door Locks Achieve Success replacement Upvc Door lock

  4. Kristina表示:

    This is my first time pay a quick visit at here and i am actually pleassant to read everthing at alone place.

  5. BradleyEdino表示:

    en kazancl? slot oyunlar? en cok kazand?ran slot oyunlar? en kazancl? slot oyunlar?

  6. вывод из запоя цены ростов-на-дону вывод из запоя цены ростов-на-дону .

  7. Fantastic read! If you’re in Canoga Park and need AC assistance, I quite recommend finding out https://blast-wiki.win/index.php/Calabasas_HVAC_Contractor_for_All_Your_Needs for trained directions

  8. At this time I am going to do my breakfast, after having my breakfast coming yet again to read additional
    news.

  9. FrankEmine表示:

    Canl? Casino Siteleri: Casino Siteleri – Casino Siteleri

  10. BradleyEdino表示:

    en kazancl? slot oyunlar? en kazancl? slot oyunlar? en cok kazand?ran slot oyunlar?

  11. FrankEmine表示:

    slot oyunlar? puf noktalar?: slot casino siteleri – slot oyunlar? puf noktalar?

  12. JasonUnsoG表示:

    https://sweetbonanza25.com/# sweet bonanza guncel

  13. Wallacefex表示:

    casino bahis siteleri: casino bahis siteleri – en guvenilir casino siteleri

  14. Katie表示:

    I was curious if you ever thought of changing the layout 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 2 images.

    Maybe you could space it out better?

  15. BradleyEdino表示:

    slot oyunlar? puf noktalar? slot siteleri az parayla cok kazandiran slot oyunlar?

  16. поставить капельницу от запоя на дому цена поставить капельницу от запоя на дому цена .

  17. вывод из запоя на дому недорого вывод из запоя на дому недорого .

  18. нарколог на дом вывод из запоя ростов нарколог на дом вывод из запоя ростов .

  19. BradleyEdino表示:

    en guvenilir casino siteleri Canl? Casino Siteleri canl? casino siteleri

  20. You actually make it seem so еasy with
    your presentation but I find this topic to be really something which I
    think I would never understand. It seems too complex and very
    broad for me. I’m looking forward fߋr your next post, I will try to get the hɑng of it!

    Alѕⲟ visit my blog post; inmate texting

  21. I’ve been struggling with bed bugs for months. This article gave me some hope! For further guidance, check out termite control near me

  22. Wilton表示:

    Now I am ready to do my breakfast, once having my breakfast coming yet again to read further news.

發佈留言

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