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,235,949 Responses

  1. RandallTaulp表示:

    how to prevent hair loss while on tamoxifen: tamoxifen 20 mg tablet – tamoxifen chemo

  2. Ronaldpsymn表示:

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

  3. Davidtes表示:

    get cheap propecia cost propecia without dr prescription order propecia pills

  4. AndrewNuh表示:

    Лечение лудомании в Алматы https://someblog.ru/

  5. WilliamRhype表示:

    http://nolvadex.life/# tamoxifen joint pain

  6. Could Double Glazed Units Near Me Be The Key For 2023’s Challenges?
    replacement Double glazed units near me

  7. WilliamRhype表示:

    https://nolvadex.life/# tamoxifen bone pain

  8. Could Jobs Working Remotely Be The Key To 2023’s Resolving?
    get paid to work From home

  9. Introduction https://wikiromandie.org/index.php?title=Utilisateur:TrimKetoBoostIngredients is a popular supplement that claims to help individuals achieve their weight loss goals by promoting ketosis in the body.

  10. WilliamRhype表示:

    http://finasteride.store/# cost of cheap propecia now

  11. Why Nobody Cares About Motor Vehicle Attorney Motor vehicle accidents

  12. car表示:

    Who Is Renault Master Key Programming And Why You Should
    Care car

  13. Ronaldpsymn表示:

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

  14. How To Become A Prosperous Misted Double Glazing Repairs Near Me When You’re Not Business-Savvy Double Glazing Unit Replacement

  15. Slot games表示:

    14 Cartoons On Player Favorite Slots Which Will Brighten Your Day Slot games

  16. You’ll Never Be Able To Figure Out This Adhd In Adults Treatment’s
    Secrets Adhd In Adults Treatment

  17. RandallTaulp表示:

    cipro online no prescription in the usa: cipro generic – purchase cipro

  18. I am really loving the theme/design of your blog.
    Do you ever run into any internet browser compatibility problems?
    A handful of my blog readers have complained about my site not working correctly in Explorer but looks great in Opera.
    Do you have any tips to help fix this issue?

  19. Hurrah, that’s what I was exploring for, what a
    material! existing here at this webpage, thanks admin of
    this site.

  20. The Unspoken Secrets Of Tommy Hilfiger Crossbody Handbag Tommy hilfiger Handbags black

  21. Why You Should Not Think About The Need To Improve Your 12kg Washing Machine best 12kg washing machine

  22. RandallTaulp表示:

    buy cytotec over the counter: Misoprostol 200 mg buy online – order cytotec online

  23. Benito表示:

    5 Killer Quora Answers On Replacement Double Glazing Units Near
    Me double glazing units near me (Benito)

  24. 7 Secrets About Why Are CSGO Skins Going Up In Price
    That Nobody Can Tell You Cs2 case; crabpigeon83.Bravejournal.net,

  25. Ronaldpsymn表示:

    https://lisinopril.network/# order lisinopril 10 mg

  26. Madeline表示:

    Seven Reasons Why Accident Case Is Important accident lawsuit (Madeline)

  27. 9 Signs That You’re The Adhd Treatment Without Medication Expert Cost Of Adhd Medication Uk

發佈留言

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