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,228,086 Responses

  1. Wow, awesome blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your website
    is wonderful, as well as the content!

  2. The 10 Most Terrifying Things About L Shaped Sectional With Chaise Sectional With
    Chaise (Perfectworld.Wiki)

  3. ClydePab表示:

    slot oyunlar? puf noktalar?: en kazancl? slot oyunlar? – slot oyunlar?

  4. ClydePab表示:

    slot siteleri: slot siteleri – slot oyunlar?

  5. LarryCoell表示:

    matadorbet bid matadorbet matadorbet giris

  6. WnrPaish表示:

    Don’t buy from sites that say their stromectol and herbs at superb savings to help minimize symptoms and feel healthier

  7. Five Killer Quora Answers On Best Accident Attorneys Accident attorney

  8. 15 Terms That Everyone Is In The Mesothelioma Lawsuits Industry Should Know mesothelioma attorney

  9. CarrollUnton表示:

    deneme bonusu veren siteler 2024 https://slot-tr.online/# az parayla cok kazandiran slot oyunlar?

  10. ClydePab表示:

    ultrabet giris: ultrabet bonus – ultrabet yeni giris 1125

  11. I visited multiple web sites however the audio feature for audio
    songs current at this web page is really wonderful.

  12. Hi to all, it’s truly a pleasant for me to visit this website, it consists
    of helpful Information.

  13. You’ll Never Guess This Automatically Folding Mobility Scooter’s Tricks automatically Folding Mobility scooter

  14. CarrollUnton表示:

    deneme bonusu veren siteler https://ultrabet-tr.online/# ultrabet bonus

  15. LarryCoell表示:

    matadorbet matadorbet.bid matadorbet

  16. What’s Next In Auto Injury Lawyers car accident lawyers
    near me; https://jisuzm.tv,

  17. Diplomi_fxMl表示:

    коломна купить диплом many-diplom77.ru .

  18. Why people still make use of to read news papers
    when in this technological globe all is presented
    on net?

  19. An impressive share! I’ve just forwarded this onto a coworker who
    has been conducting a little research on this.

    And he in fact bought me dinner because I found it for him…
    lol. So allow me to reword this…. Thanks for the meal!! But yeah,
    thanx for spending some time to talk about this subject here
    on your web site.

  20. HassanbuifS表示:

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

  21. LarryCoell表示:

    matadorbet giris matadorbet giris matadorbet bid

  22. HassanbuifS表示:

    https://slot-tr.online/# az parayla cok kazandiran slot oyunlar?
    deneme bonusu veren siteler mycbet.com

  23. 14 Misconceptions Common To Sofas Sale big couches for sale;
    eroticguide.tokyo,

  24. The Leading Reasons Why People Achieve In The Best Mesothelioma Attorney Industry Mesothelioma lawsuit (https://articlescad.com/)

  25. I got this site from my pal who told me on the topic of this web page
    and now this time I am browsing this site and reading very informative articles at this place.

  26. Diplomi_lzkn表示:

    купить диплом 2000 1russa-diploms.ru .

  27. Тут можно преобрести сейф огнеупорный сейф огнестойкий цена

發佈留言

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