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,234,849 Responses

  1. RandallTaulp表示:

    does tamoxifen make you tired: tamoxifen alternatives – tamoxifen rash pictures

  2. WilliamRhype表示:

    http://lisinopril.network/# order lisinopril online united states

  3. casino_ouKt表示:

    casino con bono de bienvenida sin deposito http://sin-deposito-peru.org/ .

  4. case skins表示:

    Technology Is Making Will CSGO Cases Go Up In Price Better
    Or Worse? case skins

  5. Where Will Treadmill Desk Uk Be One Year From This Year?
    Treadmill Under Desk

  6. Having read this I believed it was really informative.
    I appreciate you finding the time and effort to put this content together.
    I once again find myself spending a lot of time both
    reading and posting comments. But so what, it was still worthwhile!

  7. 15 Things You Didn’t Know About Misted Double Glazing Repairs Near Me double glazed Units

  8. Hello friends, fastidious article and pleasant arguments commented here, I am in fact enjoying
    by these.

  9. 10 Places To Find Double Glaze Repair Near Me Repairs Double Glazed Windows

  10. You’ll Never Guess This Cheap Mobility Scooters Near
    Me’s Secrets cheap mobility scooters near me

  11. WilliamRhype表示:

    http://cytotec.club/# buy cytotec pills online cheap

  12. RandallTaulp表示:

    order cheap propecia without insurance: order generic propecia without insurance – cost of cheap propecia

  13. Buzzwords, De-Buzzed: 10 Other Methods For Saying Window Repairman Window repairs

  14. casino_oqKt表示:

    casino con bono de bienvenida sin deposito http://www.sin-deposito-peru.org/ .

  15. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec in usa

  16. See What Compact Treadmill Incline Tricks The Celebs Are Making Use Of Compact Treadmill Incline

  17. RandallTaulp表示:

    cipro 500mg best prices: ciprofloxacin order online – ciprofloxacin

  18. RandallTaulp表示:

    purchase cytotec: Abortion pills online – buy cytotec over the counter

  19. RandallTaulp表示:

    buying generic propecia for sale: propecia tablet – cost of cheap propecia without dr prescription

  20. 10 Undisputed Reasons People Hate Volkswagen Keys volkswagen locked keys in car

  21. Ronaldpsymn表示:

    https://cytotec.club/# Misoprostol 200 mg buy online

  22. You’ll Be Unable To Guess Car Key Battery Replacement’s Tricks Car Key Battery Replacement

  23. 1win880piodo表示:

    Топовая Букмекерская контора – 1win

  24. 15 Mental Health Tests NHS Benefits That Everyone Should Be Able
    To online Mental health assessments

  25. Why We Love Best Price beko washing machines Prices Washing Machines (And You Should Also!)

  26. This History Behind Licensed Slots Will Haunt You
    For The Rest Of Your Life! Themed Slot Games

  27. Hi there, the whole thing is going well here and ofcourse every one is sharing data,
    that’s in fact excellent, keep up writing.

  28. RandallTaulp表示:

    cost cheap propecia without dr prescription: cost generic propecia for sale – buying propecia for sale

  29. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 10mg

  30. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen lawsuit

發佈留言

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