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,674,553 Responses

  1. Davidtes表示:

    tamoxifen alternatives premenopausal aromatase inhibitors tamoxifen does tamoxifen cause weight loss

  2. A synthetic USP human growth hormone (somatropin).

    My site web design clearwater florida

  3. Real-time exercise monitoring overviews your workouts.

    Feel free to surf to my webpage :: fitness equipment empire

  4. Real-time workout monitoring guides your exercises.

    Also visit my web site – fitness equipment brands

  5. Ronaldpsymn表示:

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

  6. Five Killer Quora Answers On Fiat Ducato Replacement Key fiat Ducato Replacement Key

  7. After I initially commented I appear to have clicked the -Notify me when new comments are
    added- checkbox and now each time a comment is added I receive 4 emails with the same comment.

    Is there a way you can remove me from that service? Thanks!

  8. The 9 Things Your Parents Teach You About Double Glazing Lock Repairs Double Glazing

  9. RandallTaulp表示:

    tamoxifen lawsuit: where to get nolvadex – generic tamoxifen

  10. Davidtes表示:

    buy generic ciprofloxacin buy cipro online without prescription ciprofloxacin generic price

  11. 7 Simple Tips For Rocking Your Mobile Automobile Locksmith automobile Locksmiths

  12. A Complete fitness equipment in harlingen texas Center
    Experience Anywhere, At Any Moment.

  13. bookmarks表示:

    A Full Gym Experience Anywhere, Whenever.

    Also visit my blog: bookmarks

  14. 14 Businesses Doing A Great Job At Cerebral Palsy Claim cerebral palsy law firms

  15. 10 Beautiful Graphics About Folding Mobility Scooter For 25 Stone foldable mobility Scooters uk

  16. Motor Vehicle Settlement Tips To Relax Your Everyday Lifethe Only Motor Vehicle Settlement Trick
    That Every Person Should Know motor vehicle accident lawyer

  17. Davidtes表示:

    buying cheap propecia without insurance propecia buy propecia cost

  18. WilliamRhype表示:

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

  19. Davidtes表示:

    cytotec abortion pill cytotec pills buy online cytotec online

  20. How Veterans Disability Lawyers Became The Hottest Trend
    In 2023 Veterans Disability Lawsuit

  21. RandallTaulp表示:

    nolvadex side effects: tamoxifen skin changes – tamoxifen brand name

  22. Davidtes表示:

    buy generic ciprofloxacin buy ciprofloxacin cipro ciprofloxacin

  23. Bookmarks表示:

    A Full Fitness Center Experience Anywhere, Anytime.

    Take a look at my page … Bookmarks

  24. clinic saurus表示:

    Great delivery. Great arguments. Keep up the amazing effort.

  25. Five Killer Quora Answers On Large American Fridge Freezers large American fridge Freezers

  26. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# cipro 500mg best prices

  27. Davidtes表示:

    cost of generic propecia online generic propecia cost of propecia for sale

  28. WilliamRhype表示:

    https://nolvadex.life/# where to get nolvadex

  29. Pink表示:

    There’s A Reason Why The Most Common Designer Handbags For Work Debate It’s Not As
    Black And White As You Might Think Pink

  30. service表示:

    8 Tips To Up Your Window Companies Leeds Game service

發佈回覆給「Ronaldpsymn」的留言 取消回覆

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