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

  1. The amount of a simple money mortgage is restricted.

  2. Davidtes表示:

    buying cheap propecia get cheap propecia without dr prescription propecia price

  3. RandallTaulp表示:

    buying cheap propecia without dr prescription: buy cheap propecia without a prescription – order generic propecia price

  4. juragan koin表示:

    I simply couldn’t leave your web site prior to suggesting that
    I actually enjoyed the standard information a person provide to
    your visitors? Is gonna be again continuously to check up on new posts

  5. RandallTaulp表示:

    lisinopril canada: lisinopril 20 mg uk – lisinopril generic price comparison

  6. How To Create Successful Fridge Freezer Bosch Tips From Home fridge Freezer Collection

  7. RandallTaulp表示:

    purchase cytotec: buy cytotec online fast delivery – buy misoprostol over the counter

  8. WilliamRhype表示:

    https://cytotec.club/# cytotec online

  9. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy cipro cheap

  10. 9 . What Your Parents Taught You About Green Power Mobility
    Scooters Uk green power Mobility scooters uk

  11. Reynaldo表示:

    A Look At The Secrets Of 4 Wheel Drive Mobility Scooter 4
    wheeler mobility scooter [Reynaldo]

  12. Ronaldpsymn表示:

    http://finasteride.store/# cost cheap propecia prices

  13. Ronaldpsymn表示:

    http://nolvadex.life/# nolvadex pills

  14. RandallTaulp表示:

    Misoprostol 200 mg buy online: buy cytotec in usa – buy cytotec over the counter

  15. Ronaldpsymn表示:

    https://cytotec.club/# cytotec abortion pill

  16. 15 Fun And Wacky Hobbies That’ll Make You More Successful At Greenpower Mobility Mobility Power

  17. Ronaldpsymn表示:

    http://finasteride.store/# order propecia without a prescription

  18. WilliamRhype表示:

    https://ciprofloxacin.tech/# cipro ciprofloxacin

  19. WilliamRhype表示:

    http://finasteride.store/# cost cheap propecia without a prescription

  20. Davidtes表示:

    lisinopril pill lisinopril average cost lisinopril 30 mg tablet

  21. Derrick表示:

    Why Everyone Is Talking About Glass Repair Leeds Right Now upvc door repair roundhay, Derrick,

  22. WilliamRhype表示:

    http://nolvadex.life/# femara vs tamoxifen

  23. Davidtes表示:

    over the counter lisinopril lisinopril 80 mg tablet where to buy lisinopril 2.5 mg

  24. Ronaldpsymn表示:

    http://cytotec.club/# purchase cytotec

  25. Then You’ve Found Your Erb’s Palsy Legal … Now What?
    erb’s Palsy lawyers

  26. Ronaldpsymn表示:

    https://cytotec.club/# purchase cytotec

  27. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen 20 mg

  28. Ronaldpsymn表示:

    http://nolvadex.life/# tamoxifen cost

  29. WilliamRhype表示:

    https://nolvadex.life/# effexor and tamoxifen

  30. WilliamRhype表示:

    https://finasteride.store/# order generic propecia for sale

發佈留言

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