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,144,623 Responses

  1. The 10 Most Scariest Things About Slot Bonuses video slots (bookmarkzones.Trade)

  2. You’ll Never Be Able To Figure Out This 2 In 1 Highchair’s Tricks 2
    In 1 Highchair – Emplois.Fhpmco.Fr

  3. From Around The Web Twenty Amazing Infographics About Case Opening Sites CSGO Case Operation Bravo

  4. So You’ve Bought Car Accident Law … Now What? car Accident Lawyer

  5. A Provocative Remark About Slot Machine Games evoplay slots games

  6. Joint Hero表示:

    Do you mind if I quote a couple of your posts as long as I provide
    credit and sources back to your website? My blog is in the exact same niche as yours and my
    users would really benefit from some of the information you present here.

    Please let me know if this ok with you. Thank you!

  7. lawsuit表示:

    10 Things You’ve Learned In Kindergarden They’ll
    Help You Understand Federal Employers lawsuit

  8. Foldable Under Desk Treadmill Tools To Make
    Your Everyday Lifethe Only Foldable Under Desk Treadmill Trick That Should Be Used By Everyone
    Be Able To Foldable Under Desk Treadmill

  9. 10 Misleading Answers To Common Online Shopping Top
    7 Questions: Do You Know The Right Answers? Residential American Flagpole

  10. Slots For Fun表示:

    What’s The Current Job Market For Slots For Fun Professionals?
    Slots For Fun

  11. The Most Popular Slot Apps Is Gurus. 3 Things Best payouts
    slots, https://www.hulkshare.Com/cancercotton56/,

  12. Window Repair表示:

    Guide To Window Repair: The Intermediate Guide To Window
    Repair Window Repair

  13. Drug information. Short-Term Effects.
    can you buy generic indomethacin without insurance
    Some about pills. Get information here.

  14. The 10 Most Terrifying Things About Upvc Door Repairs Near Me upvc Door repairs Near me

  15. Lilly表示:

    Slot Machine Games: What No One Is Talking About evoplay slots rewarding [Lilly]

  16. profile表示:

    Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you!

    By the way, how can we communicate?

  17. Birth Defect Case Tips From The Top In The Industry birth defect lawsuits

  18. Amazing loads of excellent material.

  19. lawyers表示:

    Medical Malpractice Attorneys Is The Next Hot Thing In Medical Malpractice
    Attorneys lawyers

  20. 11 Methods To Redesign Completely Your Upvc Window Repairs window repairs near Me

  21. slot strategy表示:

    12 Statistics About Slot Strategies To Inspire You To Look More Discerning Around The Cooler Water Cooler slot strategy

  22. 20 Trailblazers Lead The Way In Glass Repair Cambridge Double Glazing Near Me

  23. You definitely made the point!

  24. 4 Dirty Little Secrets About Double Buggy Industry Double Buggy
    Industry tandem double buggy

  25. 5 Malpractice Settlement Lessons Learned From The Pros malpractice Lawsuit

  26. Your mode of describing all in this article is truly fastidious,
    all be capable of simply know it, Thanks a lot https://www.cliniquedelenfant.ca/formations-et-conferences/

  27. wd808 slot表示:

    I have read so many content on the topic of the blogger lovers
    however this article is really a good article, keep it up.

  28. Why Auto Accident Lawsuit Isn’t As Easy As You Imagine Auto Accident law firm (https://freemaple.Today/)

發佈留言

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