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,232,374 Responses

  1. What Do You Think? Heck What Exactly Is Designer Bags?
    online designer handbags

  2. Say “Yes” To These 5 Bunk Bed Online Store Tips Bunk Beds sale

  3. You’ll Never Guess This Window Repair Near Me’s Secrets Window repair near me

  4. Zulma表示:

    The jackpot rose to $687 million with a money value of $332.three million.

    My webpage … https://www.istitutomorgagni.it/the-ultimate-guide-to-winning-the-lottery-jackpot/

  5. RandallTaulp表示:

    buy propecia pills: buy propecia no prescription – order propecia without prescription

  6. Responsible For A Double Glazed Window Repairs Near Me Budget?
    12 Top Notch Ways To Spend Your Money Double glazed wooden windows

  7. WilliamRhype表示:

    https://nolvadex.life/# tamoxifen estrogen

  8. You’ll Be Unable To Guess Railroad Injuries Settlement’s Secrets railroad injuries – http://m.042-527-9574.1004114.co.kr/,

  9. It’s perfect time to make a ffew plans for thе lonng runn and it’s time
    to be happy. I haᴠe read this pst and if I may just I wanmt to suggest yoou feew attention-grabbing issues or
    tips. Perhuaps you coսld write next articles referring
    to this ɑrtіcle. I desire to read even more issues approximately
    it!

    Feel free to visit myy sіte – ck4d link aⅼternatif – http://www.google.co.ck,

  10. Davidtes表示:

    nolvadex only pct tamoxifen generic tamoxifen postmenopausal

  11. RandallTaulp表示:

    tamoxifen effectiveness: natural alternatives to tamoxifen – who should take tamoxifen

  12. The Largest Issue That Comes With Railroad Injuries Law, And
    How You Can Repair It Railroad injuries lawyers

  13. The 10 Scariest Things About Double Glazing Near Me Double Glazing Near Me

  14. Davidtes表示:

    buy cytotec pills online cheap buy misoprostol over the counter п»їcytotec pills online

  15. Ronaldpsymn表示:

    https://finasteride.store/# cost cheap propecia tablets

  16. RandallTaulp表示:

    generic propecia without rx: cost cheap propecia without insurance – cost propecia online

  17. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# buy ciprofloxacin

  18. Enough Already! 15 Things About Car Key Fob Replacement We’re Sick Of Hearing mobile car Key replacement Near me

  19. Ronaldpsymn表示:

    http://lisinopril.network/# lisinopril medication otc

  20. RandallTaulp表示:

    buy cipro online without prescription: ciprofloxacin generic – where can i buy cipro online

  21. 10 Strategies To Build Your Program A Car Key Empire how to program a car key

  22. Robert表示:

    You’ll Never Guess This Link Daftar Gotogel’s Benefits
    link daftar gotogel (Robert)

  23. Software Key表示:

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time
    a comment is added I get several emails with the same comment.
    Is there any way you can remove me from that service?

    Thank you!

  24. Tommy Hilfiger Bookbag Tips From The Top In The Business bag tommy hilfiger sale

  25. Ronaldpsymn表示:

    http://lisinopril.network/# prinivil 5mg tablet

  26. How To Explain Mesothelioma Claim To Your Grandparents Mesothelioma Law

  27. I read this article fully on the topic of the comparison of latest and previous technologies, it’s amazing
    article.

  28. Davidtes表示:

    cipro for sale cipro purchase cipro

  29. Who Is Window Repair Near And Why You Should Care window repair near me

發佈留言

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