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,228,768 Responses

  1. RandallTaulp表示:

    tamoxifen buy: tamoxifen and antidepressants – tamoxifen endometriosis

  2. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# ciprofloxacin mail online

  3. WilliamRhype表示:

    http://cytotec.club/# order cytotec online

  4. Hey very interesting blog!

    Here is my blog post; cancer research

  5. RandallTaulp表示:

    cost propecia for sale: propecia price – cost generic propecia without prescription

  6. WilliamRhype表示:

    https://cytotec.club/# order cytotec online

  7. Вы можете использовать традиционные методы, такие как электронные
    кошельки, кредитные карты и банковские переводы для осуществления платежей.

  8. Ronaldpsymn表示:

    https://finasteride.store/# get propecia tablets

  9. Davidtes表示:

    cost cheap propecia without prescription cost propecia without prescription cost generic propecia pill

  10. 5 Killer Qora’s Answers To Double Glazed Windows Leeds Upvc door Repairs Meanwood

  11. Ronaldpsymn表示:

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

  12. Maura表示:

    7 Tricks To Help Make The Most Out Of Your Slot Features progressive slots,
    Maura,

  13. Ronaldpsymn表示:

    https://finasteride.store/# buying propecia price

  14. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# antibiotics cipro

  15. WilliamRhype表示:

    http://finasteride.store/# buy propecia pills

  16. Undeniably consider that that you stated. Your favorite
    justification seemed to be on the internet the simplest thing to understand
    of. I say to you, I certainly get irked while other people consider worries that they
    plainly do not recognize about. You managed How to interpret forex signals hit the nail upon the top and also defined out
    the whole thing without having side effect ,
    folks could take a signal. Will probably be again to get more.

    Thanks

  17. Heya i’m for the first time here. I came across this board and I
    find It truly useful & it helped me out much. I hope to
    give something back and aid others like you aided me.

  18. Ronaldpsymn表示:

    http://finasteride.store/# cost of propecia online

  19. tokyo escort表示:

    Hi there, just became aware of your blog through Google, and found that it
    is truly informative. I am gonna watch out for
    brussels. I’ll be grateful if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!

  20. WilliamRhype表示:

    http://lisinopril.network/# cost of lisinopril in canada

  21. Davidtes表示:

    order cytotec online buy misoprostol over the counter buy cytotec pills

  22. 온카 조작表示:

    I don’t know what were the codes for. But I truly appreciate the effort you exert to your blog.

  23. Janell表示:

    15 Documentaries That Are Best About Window Companies Leeds door repairs leeds (Janell)

  24. WilliamRhype表示:

    http://ciprofloxacin.tech/# ciprofloxacin 500mg buy online

  25. Davidtes表示:

    cytotec pills buy online buy cytotec in usa buy cytotec pills online cheap

  26. RandallTaulp表示:

    nolvadex gynecomastia: tamoxifen dose – tamoxifen and grapefruit

  27. Santo表示:

    A Guide To Glass Repair Leeds From Start To
    Finish double glazing repair (Santo)

  28. Hi would you mind letting me know which web host you’re working with?

    I’ve loaded your blog in 3 completely different browsers and I must
    say this blog loads a lot faster then most. Can you recommend a good hosting provider at a honest price?
    Thanks, I appreciate it!

  29. Three Wheel Mobility Scooter’s History Of Three Wheel Mobility Scooter In 10 Milestones 3 wheeled mobility Scooters

  30. chandal juventus 2023

發佈留言

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