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,227,713 Responses

  1. Presenting our selection of Short Bedtime Stories, a charming trip through children’s dreams. Every narrative is skillfully written to enthrall young readers and transport them to fantastical settings and touching tales. These are the ideal stories to curl up with when the lights go off if you want to conclude the day with amazement and happiness.

  2. window Repair表示:

    Are You Responsible For An Upvc Window Repairs Budget? 12 Best Ways
    To Spend Your Money window Repair

  3. RandallTaulp表示:

    cipro online no prescription in the usa: cipro generic – ciprofloxacin order online

  4. vimeo.Com表示:

    15 Lessons Your Boss Wishes You Knew About Waitrose Groceries Online Shopping Uk high-capacity Aluminum reel (vimeo.Com)

  5. Ronnie表示:

    Could Lg Side By Side Fridge Freezer Be The Key To 2023’s Resolving?
    short side by side fridge freezer, Ronnie,

  6. Looking Into The Future What’s The Window Repair Industry Look Like In 10 Years?

    double glazed window repairs near me

  7. Anya表示:

    Tips For Explaining Treadmills Sale To Your Boss treadmill sale (Anya)

  8. Is Private Diagnosis ADHD As Important As Everyone Says?
    Adult adhd private assessment

  9. The No. 1 Question Everybody Working In Become A Rep Avon Should Know How To Answer become An avon Representative

  10. 7 Simple Strategies To Completely Rocking Your Motorcycle
    Accident Compensation motorcycle accident law firm

  11. WilliamRhype表示:

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

  12. Davidtes表示:

    buy cipro online canada ciprofloxacin 500mg buy online cipro pharmacy

  13. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 10 mg over the counter

  14. Reenergized
    4434 Pacific Coast Hwy,
    ᒪong Beach, ᏟA 90804, United Stateѕ
    562-689-9888
    Wavi brain scan test accuracy

  15. Hello! I could have sworn I’ve visited this website before
    but after looking at some of the posts I realized it’s new to
    me. Anyways, I’m certainly happy I stumbled upon it and I’ll be bookmarking it and checking back
    often!

  16. Why We Love Drive Folding Scooter (And You Should Too!)
    heavy duty foldable mobility scooter – promarket.in.ua

  17. 9 Things Your Parents Taught You About Adhd In Adult Women Symptoms
    adhd in adult Women symptoms

  18. RandallTaulp表示:

    buy cytotec pills online cheap: Misoprostol 200 mg buy online – cytotec abortion pill

  19. Davidtes表示:

    buy cytotec over the counter buy cytotec online cytotec pills buy online

  20. WilliamRhype表示:

    http://finasteride.store/# get cheap propecia without dr prescription

  21. Why Nobody Cares About Shopping Online Uk Ski Plexi Scraper

  22. An Easy-To-Follow Guide To Choosing Your Private ADHD Assessment how
    to get a private Adhd assessment (https://rossoneri.ge)

  23. WilliamRhype表示:

    http://nolvadex.life/# what is tamoxifen used for

  24. The 10 Most Scariest Things About Link Login Gotogel Link Login gotogel

  25. The Reasons 4 Wheel Mobility Scooter Electric Scooters Is
    Everyone’s Obsession In 2023 Electric Mobility Scooter

  26. Ronaldpsymn表示:

    https://finasteride.store/# cost generic propecia online

  27. Davidtes表示:

    cheap propecia prices cheap propecia prices cost cheap propecia no prescription

  28. Uk Online Phone Shopping Sites: The Good, The Bad, And The Ugly Wowkk Tub Faucet

  29. Privatehd.org表示:

    20 Fun Informational Facts About Counter Strike Pc case glove – Privatehd.org

發佈留言

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