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,232,928 Responses

  1. Davidtes表示:

    Misoprostol 200 mg buy online purchase cytotec cytotec online

  2. RandallTaulp表示:

    nolvadex 10mg: tamoxifen joint pain – tamoxifen for breast cancer prevention

  3. java burn表示:

    Hello There. I found your weblog the usage of msn. This is an extremely well written article.
    I’ll be sure to bookmark it and return to
    read more of your helpful info. Thank you for the
    post. I’ll definitely comeback.

  4. Why You Should Concentrate On Improving Mesothelioma Settlement asbestos Settlement

  5. Davidtes表示:

    lisinopril 420 1g lisinopril medication otc price of lisinopril generic

  6. Pretty part of content. I simply stumbled upon your blog
    and in accession capital to assert that I acquire in fact loved account your weblog posts.
    Any way I’ll be subscribing on your augment and even I success you get admission to
    constantly fast.

  7. An Adventure Back In Time What People Said About How To Get Cases In CS GO 20 Years Ago Case opening – maps.google.no,

  8. Oren表示:

    Many Of The Common Errors People Make When Using Mesothelioma Navy
    Compensation mesothelioma compensation claim (Oren)

  9. The 10 Most Terrifying Things About Machine Espresso cafe espresso Machine

  10. Marsha表示:

    The Underrated Companies To In The Double Glazing Doctor Near Me
    Industry double glazed window suppliers near me (Marsha)

  11. Ronaldpsymn表示:

    https://nolvadex.life/# buy nolvadex online

  12. Davidtes表示:

    tamoxifen moa tamoxifen pct nolvadex

  13. Davidtes表示:

    cytotec online п»їcytotec pills online cytotec pills buy online

  14. Davidtes表示:

    buy lisinopril 20 mg lisinopril 10 mg order online buy lisinopril 20 mg online usa

  15. Davidtes表示:

    lisinopril tabs 10mg lisinopril 50 mg lisinopril pharmacy online

  16. I constantly emailed this webpage post page to all my
    friends, as if like to read it then my contacts will too.

  17. The 9 Things Your Parents Taught You About Private Psychiatry Assessment Psychiatry Assessment

  18. WilliamRhype表示:

    http://lisinopril.network/# buy 40 mg lisinopril

  19. 10 Things You Learned In Preschool, That’ll Aid You In Mesothelioma Case
    Mesothelioma lawyer

  20. My developeг is trying to persuade me tо move to .net from PHP.

    I havе аlᴡays disliked the idea because of the costs.
    But he’s tryiօng none the less. I’ve been using Movable-type on various weЬsіjtes for about
    a yеar and am nervous about swіtching to another platform.
    I have heard excellent things ɑbout blogengine.net. Is there a way I can transfer all
    mу wordpress contеnt into it? Αny һelp would be greatly
    apprecіated!

    Feеel free tto surf to my page; kunjungi situs

  21. Freya表示:

    5 Killer Quora Answers On Private Psychiatrist Assessment psychiatrist assessment (Freya)

  22. 20 Fun Informational Facts About Repairs To Double Glazed Windows Window repairs

  23. The 10 Most Terrifying Things About Auto Accident
    Law Firm Kansas City Auto Accident Lawyer

  24. Davidtes表示:

    propecia buy cost cheap propecia without dr prescription order propecia without prescription

  25. RandallTaulp表示:

    how to prevent hair loss while on tamoxifen: tamoxifen therapy – tamoxifen 20 mg tablet

  26. The Best Tips You’ll Ever Receive On Which Beko Washing Machine

  27. WilliamRhype表示:

    https://ciprofloxacin.tech/# ciprofloxacin order online

發佈留言

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