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,226,343 Responses

  1. Ronaldpsymn表示:

    http://nolvadex.life/# clomid nolvadex

  2. І’m impressed, I һave to admit. Seldom do I cme acfross ɑ blog that’s equally educative and engaging, ɑnd let me tell you, yoou have
    hit tһе nail օn the head. Tһe issue іs somethin not
    enough men ɑnd women aгe speaking intelligently ɑbout.
    I’m very happʏ that I cаme across thius in my search fօr something
    concеrning tһis.

    Review my web page uutisia internetistä tänään

  3. WilliamRhype表示:

    https://nolvadex.life/# nolvadex steroids

  4. Ronaldpsymn表示:

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

  5. Davidtes表示:

    generic propecia online order cheap propecia buying generic propecia tablets

  6. All-Inclusive Guide To Irobot Vacuum robot vacuum for stairs (morgan-foged-4.mdwrite.net)

  7. WilliamRhype表示:

    https://cytotec.club/# buy cytotec online fast delivery

  8. Davidtes表示:

    tamoxifen pill nolvadex gynecomastia nolvadex gynecomastia

  9. WilliamRhype表示:

    http://cytotec.club/# buy cytotec

  10. 13 Things About Test For Adult ADHD You May Not Have Considered adult testing adhd

  11. Mathias表示:

    7 Tricks To Help Make The Greatest Use Of Your
    Diagnostic Check Near Me diagnostic car (Mathias)

  12. Davidtes表示:

    propecia pills cost of propecia without rx order cheap propecia without insurance

  13. A Productive Rant Concerning Uk Online Shopping Sites For
    Electronics Charlotte Pipe 4 Vent Tee

  14. RandallTaulp表示:

    lisinopril metoprolol: lisinopril 20 mg generic – lisinopril price 10 mg

  15. 10-Pinterest Accounts You Should Follow About Bean To Cup Coffee Machine
    bean-to-cup coffee machines

  16. Davidtes表示:

    buy cipro where can i buy cipro online purchase cipro

  17. 10 Things That Your Family Taught You About Bmw Replacement Car Keys bmw Replacement car keys

  18. You’ll Never Guess This Best Robot Vacuum Self
    Emptying’s Secrets best robot vacuum Self empty

  19. Where Can You Get The Most Effective Ford Key Cutting Information? Ford Transit Key Replacement

  20. RandallTaulp表示:

    lisinopril 30 mg cost: lisinopril generic brand – lisinopril 20 mg uk

  21. RandallTaulp表示:

    buying generic propecia tablets: propecia pill – get generic propecia prices

  22. Latonya表示:

    A List Of Common Errors That People Make With Repairs To
    Upvc Windows double glazing repairs (Latonya)

  23. Davidtes表示:

    lisinopril 5mg buy zestoretic online lisinopril 20 pills

  24. WilliamRhype表示:

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

  25. Audi A3 Car Key Battery: The Ultimate Guide To Audi
    A3 Car Key Battery Replacement car keys Audi

  26. Guide To Robot Vacuum And Mop With Self Emptying: The Intermediate Guide For Robot Vacuum And Mop
    With Self Emptying Robot Vacuum And Mop With Self Empty

  27. RandallTaulp表示:

    is nolvadex legal: buy nolvadex online – how to lose weight on tamoxifen

  28. This is my first time go to see at here and i am actually pleassant to read all at single
    place.

發佈留言

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