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...

909,781 Responses

  1. Guide To Fiat Punto Key Replacement: The Intermediate Guide
    Towards Fiat Punto Key Replacement fiat punto key Replacement

  2. replaced表示:

    8 Tips To Improve Your Saab Replacement Key Game replaced

  3. WilliamThity表示:

    cheap canadian pharmacy: canadian pharmacy india – canadian pharmacy ratings

  4. DavidThynC表示:

    buy prescription drugs from india indian pharmacies safe reputable indian online pharmacy

  5. WilliamThity表示:

    canadian prescription drugstore review: order medication without prescription – quality prescription drugs canada

  6. Declan表示:

    10 Unexpected Tier 2 Links Seo Tips tier backlinks (Declan)

  7. DonaldFrown表示:

    best online pharmacies in mexico: medication from mexico pharmacy – mexican pharmaceuticals online

  8. Everything is very open with a really clear explanation of the challenges.
    It was definitely informative. Your site is useful. Thank you for sharing!

    my blog post: assessment development center

  9. analsex.tube表示:

    and yet, take a look at this scene now, inside movie, but not
    at the hype, as in analsex.tube, and it will
    appear to you not excessively successful.

  10. 12 Facts About Porsche Spare Key To Make You Take A Look At Other People porsche key Replacement

  11. birth defect表示:

    You’ll Be Unable To Guess Birth Defect Settlement’s Benefits birth defect

  12. josephskeeds表示:

    Советую Вам посетить сайт, на котором есть много статей по этому вопросу.
    Разработка Сайтов разработка.site – это творческий процесс, требующий умений, мастерства и опыта. Специалисты определяют потребности заказчика и превращают их в функциональные веб-сайты.

  13. WilliamThity表示:

    mail order pharmacy india: india online pharmacy – mail order pharmacy india

  14. 5 Motives Best Case Opening Site CSGO Can Be A Beneficial Thing Case revolution

  15. Firm表示:

    20 Resources To Make You More Effective At Railroad Injuries Attorneys
    Firm

  16. DavidThynC表示:

    purple pharmacy mexico price list mexican pharmacy п»їbest mexican online pharmacies

  17. Let’s Get It Out Of The Way! 15 Things About Veterans Disability Lawyer We’re Tired
    Of Hearing Sweetwater veterans disability attorney

  18. Repairs表示:

    The Reason Why Double Glazing Door Repairs Near Me Is The Most-Wanted Item In 2023 Repairs

  19. 5 Killer Quora Answers On Window Repairs Near Me window repairs near me (promarket.In.ua)

  20. Seven Explanations On Why Adhd In Adults Treatment Is Important
    treating adult adhd

  21. DavidThynC表示:

    foreign pharmacy no prescription cheapest pharmacy promo code for canadian pharmacy meds

  22. lzdsxxb.com表示:

    What Do You Do To Know If You’re Prepared To Go After Diagnostics Automotive how much are Car diagnostic tests – lzdsxxb.com,

  23. WilliamThity表示:

    Online medicine home delivery: top 10 online pharmacy in india – indian pharmacy

  24. DavidThynC表示:

    top online pharmacy india Online medicine home delivery Online medicine order

  25. The 10 Scariest Things About Cheapest Beko Washing Machine cheapest Beko Washing machines

  26. reprogramming表示:

    12 Companies Leading The Way In Car Key Program Near Me reprogramming

  27. Felipevon表示:

    https://pharmmexico.online/# mexican online pharmacies prescription drugs

  28. Felipevon表示:

    https://pharmcanada.shop/# safe canadian pharmacy

  29. DavidThynC表示:

    legit canadian online pharmacy ordering drugs from canada cheap canadian pharmacy

  30. Donaldelaps表示:

    no prescription needed: pharmacy with no prescription – buying drugs online no prescription

發佈留言

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