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,523,852 Responses

  1. Davidmoits表示:

    easy canadian pharm: canadian pharmacy meds – easy canadian pharm

  2. WillieRap表示:

    discount drug mart pharmacy: discount drugs – discount drug mart

  3. MichaelGeony表示:

    canadian online pharmacy no prescription http://discountdrugmart.pro/# international pharmacy no prescription

  4. Thanks for sharing your info. I really appreciate your efforts and I
    will be waiting for your next write ups thanks once again.

  5. MarkWat表示:

    Приветствую. Подскажите, где почитатьразные блоги о займах? Сейчас читаю – arenda-kapital.ru

  6. MichaelGeony表示:

    canadian pharmacy no prescription https://xxlmexicanpharm.shop/# xxl mexican pharm

  7. WillieRap表示:

    easy canadian pharm: easy canadian pharm – buy canadian drugs

  8. осмотр гинеколога порно http://www.ginekolog-rukoeb1.ru .

  9. 1win_wtOa表示:

    официальный сайт 1win http://www.fabc.com.kg .

  10. MichaelGeony表示:

    canadian pharmacy coupon code https://easycanadianpharm.com/# easy canadian pharm

  11. Normanlix表示:

    1xBet provides a wide array of bonuses and promotions for its customers in 2025. These offerings include welcome bonuses, reload bonuses, cashback bonuses, and more. The welcome bonus is typically available to new customers making their first deposit, while reload bonuses cater to existing customers who make additional deposits https://aledia.es/pag/codigo_de_bono_de_dinero_gratis_de_1xbet.html

  12. MichaelIncap表示:

    dark market list https://github.com/darknetmarketslinks/darknetmarketlinks – the dark internet deep web sites

  13. Florence表示:

    5 Killer Quora Answers On Mental Health Assessment nuffield mental health
    assessment [Florence]

  14. MichaelGeony表示:

    rxpharmacycoupons http://megaindiapharm.com/# Mega India Pharm

  15. Rodneytus表示:

    darknet drug store https://github.com/darkwebmarketslinks/darkwebmarkets – darkmarkets how to access dark web

  16. Appreciate this post. Let me try it out.

  17. WillieRap表示:

    xxl mexican pharm: xxl mexican pharm – mexico drug stores pharmacies

  18. china表示:

    You’ve made some really good points there. I looked on the internet for more information about the issue and found
    most people will go along with your views on this site.

  19. MarkWat表示:

    Всем привет! Подскажите, где найтиразные статьи о кредитах? Сейчас читаю – https://kredit116.ru

  20. losing weight表示:

    Hi Dear, are you actually visiting this web page
    regularly, if so afterward you will absolutely take pleasant know-how.

發佈留言

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