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,107 Responses

  1. Hey There. I discovered your blog the use of msn. This
    is a very smartly written article. I will be
    sure to bookmark it and return to learn extra of your useful info.
    Thank you for the post. I’ll certainly comeback.

  2. JasonUnsoG表示:

    https://casinositeleri25.com/# Canl? Casino Siteleri

  3. Bettie Nguyen表示:

    This was beautifully organized. Discover more at hardwood floor refinishing near me

  4. Andre Larson表示:

    This was very enlightening. More at drug detox omaha

  5. web tasarım表示:

    Helpful information, Cheers!

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

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

  8. Wallacefex表示:

    en kazancl? slot oyunlar?: slot oyunlar? – en kazancl? slot oyunlar?

  9. Wallacefex表示:

    sweet bonanza demo oyna: sweet bonanza oyna – sweet bonanza kazanma saatleri

  10. вывод из запоя круглосуточно ростов https://vyvod-iz-zapoya-rostov27.ru/ .

  11. Troy McGuire表示:

    Você abordou tudo que eu precisava saber sobre a Riviera Francesa! Vou explorar mais conteúdo em Roteiros exclusivos Riviera Francesa

  12. This is a topic that’s near to my heart… Best wishes!
    Exactly where are your contact details though?

  13. This post presents clear idea designed for the new viewers of blogging, that genuinely how to do running a blog.

    My blog auto phone case

  14. Dannyfut表示:

    tor markets links darknet site dark web drug marketplace

  15. seobiawak表示:

    Pembahasan yang bagus! Untuk informasi tambahan, Anda bisa mengunjungi KADEKTOTO SLOT GACOR.

  16. FrankEmine表示:

    yat?r?ms?z deneme bonusu veren siteler: deneme bonusu veren siteler yeni – yat?r?ms?z deneme bonusu veren siteler

  17. Caleb Hart表示:

    This was quite informative. For more, visit mortgage broker Memphis

  18. Virgie Blake表示:

    This was quite informative. For more, visit roof repair near me

  19. вывод из запоя в стационаре ростова https://vyvod-iz-zapoya-rostov27.ru/ .

  20. seobiawak表示:

    Artikel yang sangat menarik! Saya juga baru membaca artikel terkait di KADEKTOTO SLOT GACOR yang membahas topik serupa.

  21. Zelda表示:

    Thanks a bunch for sharing this with all folks you actually realize what you’re
    speaking approximately! Bookmarked. Kindly also seek advice from my site =).

    We may have a link trade agreement between us

  22. Убедительная просьба дополнительно уточнять указанные данные по электронной почте или контактным телефонам.

  23. penis表示:

    Thank you, I’ve just been searching for information approximately this subject for a
    while and yours is the greatest I have discovered till now.
    However, what about the bottom line? Are you certain about the
    source?

發佈留言

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