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,367,120 Responses

  1. Davidtes表示:

    lisinopril 10 mg price in india lisinopril 200mg lisinopril 5 mg

  2. Could Mesothelioma Legal Question Be The Key To 2023’s Resolving?
    asbestos claim

  3. birth defect表示:

    See What Birth Defect Lawsuit Tricks The Celebs Are Making Use Of birth defect

  4. A List Of Common Errors That People Make With Cheapest Folding Mobility Scooter best folding Mobility Scooter

  5. Ten Mobile Car Locksmiths That Really Make Your Life
    Better mobile car locksmiths

  6. 9 Lessons Your Parents Taught You About Veterans Disability Lawsuit Veterans Disability lawsuit

  7. Davidtes表示:

    lisinopril cost 5mg zestoretic generic price of zestril

  8. 15 Presents For The Selling Beauty Products From Home Uk Lover In Your Life sell avon cosmetics (Web011.dmonster.kr)

  9. WilliamRhype表示:

    http://ciprofloxacin.tech/# ciprofloxacin over the counter

  10. vo_ntorg_diot表示:

    Воєнторг
    19. Тактические ремни и пояса для фиксации снаряжения
    куртка зимова хелікон https://voentorgklyp.kiev.ua/odyag/kurtky/kurtky-helikon-tex/ .

  11. Ronaldpsymn表示:

    https://cytotec.club/# Abortion pills online

  12. The 10 Scariest Things About Mental Health Assessment Near
    Me mental health assessment Near me

  13. Highly descriptive post, I liked that a lot. Will there be a part 2?

  14. Sv388dad表示:

    What’s up, its good post regarding media print, we all understand media is a wonderful source of facts.

  15. What Do You Need To Know To Be Are Ready For Double Glazing Windows Leeds
    upvc door repairs roundhay (Doodleordie.com)

  16. The Reasons You’re Not Successing At Bentley Key Fob bentley car keys

  17. Danial表示:

    10 Facts About Car Key Replacement Service Near
    Me That Can Instantly Put You In An Optimistic Mood remote car key replacement – Danial

  18. Nine Things That Your Parent Taught You About Mini Sectional Sofa Mini sectional sofa

  19. This design is wicked! You definitely know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start
    my own blog (well, almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it.
    Too cool!

  20. Alfonzo表示:

    Your method of explaining everything in this paragraph is truly
    nice, every one be able to without difficulty know it, Thanks a lot https://t.me/s/ventilation_terrebonne

  21. Ronaldpsymn表示:

    http://lisinopril.network/# prinivil generic

  22. What’s The Job Market For Double Glazed Repairs Near Me Professionals Like?

    double glazed repairs Near me

  23. Three Common Reasons Your Slot Strategies Isn’t Working (And How To Fix It) slot payouts (m.tjtechno.Co.Kr)

  24. A Provocative Remark About Upvc Window Repairs Window Repairs Near Me

  25. Ronaldpsymn表示:

    https://finasteride.store/# propecia no prescription

  26. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec

  27. Search Engine Optimisation Services Tools To Improve Your Everyday Lifethe Only Search Engine Optimisation Services Trick That Everybody Should Learn Search engine optimisation services

  28. nearby表示:

    What’s Everyone Talking About Double Glazing London Today nearby

  29. “The Ultimate Cheat Sheet On Backlink Seo Software backlink Submission software

  30. WilliamRhype表示:

    http://finasteride.store/# get generic propecia without rx

發佈留言

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