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

912,593 Responses

  1. Davidtes表示:

    buy cytotec over the counter buy cytotec in usa cytotec online

  2. RandallTaulp表示:

    propecia prices: order propecia without rx – order cheap propecia no prescription

  3. 20 Important Questions To ASK ABOUT Double Glazing Repair Cost Before You Decide To Purchase It window repairs [nagievonline.com]

  4. 14 Cartoons On Delonghi Nespresso Machine That’ll Brighten Your Day pink nespresso machine

  5. What Is CSGO Opening Sites And Why Is Everyone Speakin’ About It?
    case Operation Bravo

  6. Whoa! This blog looks just like my old one! It’s on a entirely
    different topic but it has pretty much the same layout and design. Outstanding choice of colors!

  7. Davidtes表示:

    order propecia no prescription get generic propecia without a prescription cost generic propecia no prescription

  8. Davidtes表示:

    lisinopril pills 2.5 mg drug prices lisinopril lisinopril 3973

  9. RandallTaulp表示:

    nolvadex vs clomid: tamoxifen premenopausal – nolvadex pills

  10. A Step-by-Step Guide To Selecting The Right Adhd Treatments Treatment for adhd in adults Uk

  11. WilliamRhype表示:

    https://finasteride.store/# cost of cheap propecia prices

  12. Why You Should Be Working With This Double Bunk Bed sturdy bunk beds

  13. Eve表示:

    Five Killer Quora Answers To Replacement Double Glazing Units Near Me replacement double glazing units near me (Eve)

  14. Wally表示:

    This Week’s Top Stories Concerning Birth Defect Claim birth defect law firms (Wally)

  15. RandallTaulp表示:

    cytotec pills online: buy cytotec online – order cytotec online

  16. How Ghost Immobiliser Problems Has Become The Top Trend On Social Media ghost Immobiliser installation

  17. Hello i am kavin, its my first time to commenting anywhere, when i read this piece of writing i thought i could
    also make comment due to this brilliant article.

  18. Thanks in support of sharing such a fastidious thinking, piece of writing is nice, thats
    why i have read it entirely

    my site; Explore forex trading platforms

  19. Dustin表示:

    The 10 Most Terrifying Things About Emergency Car Key
    Replacement emergency car key replacement (Dustin)

  20. RandallTaulp表示:

    get propecia without dr prescription: cost generic propecia online – how cÉ‘n i get cheap propecia pills

  21. Ronaldpsymn表示:

    https://nolvadex.life/# tamoxifen cancer

  22. If you have noticed your child’s struggles with ADHD it could be time to take an immediate Adhd in adults symptoms test symptoms test.
    These tests are crucial because they let you and your physician determine which treatment option is best for your child.

  23. You’ll Never Be Able To Figure Out This Mobile
    Car Diagnostics’s Benefits car diagnostics near Me

  24. Nine Things That Your Parent Taught You About
    Mesothelioma Litigation Mesothelioma litigation

  25. Five Things You’re Not Sure About About Diagnostics
    Near Me mobile diagnostics near me

  26. Double Glazing Companies Near Me Tips To Relax Your Daily Life Double Glazing
    Companies Near Me Trick That Everybody Should Know double glazing companies Near me

  27. WilliamRhype表示:

    http://lisinopril.network/# can i buy generic lisinopril online

  28. RandallTaulp表示:

    lisinopril 10mg daily: buy lisinopril online no prescription – 30mg lisinopril

  29. Search Engine Optimization Techniques To Simplify Your
    Daily Lifethe One Search Engine Optimization Trick That Everyone Should Learn search engine optimization Marketing

  30. Appreciate it. Numerous knowledge.

發佈留言

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