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,374,932 Responses

  1. Incredible! This blog looks exactly like my old one!

    It’s on a completely different subject but it has pretty much the same layout and design.
    Outstanding choice of colors!

  2. What’s The Job Market For Replacement Bmw Key Professionals Like?
    replacement bmw Key

  3. Ruby Gibson表示:

    Just moved with the help of moving company queens

  4. "Free Gift"表示:

    Hі colleagues, nicce ppost annd goodd aгgumkents commented here, I amm trujly enjoying bby these.

    Loook ito mmy homepage … “Free Gift”

  5. Nikefut表示:

    deep web sites deep web markets tor markets links

  6. Incredible! This blog looks just like my old one!
    It’s on a completely different subject but it has pretty much the same layout and design. Excellent choice of colors!

    Look at my page; เครดิตฟรีไม่มีเงื่อนไข

  7. Harvey Moss表示:

    Kudos to all the hard-working movers helping families relocate; it’s no small task! Make sure you connect with ### anykeyword ### when considering your next move from long distance movers sarasota

  8. Local moves can be challenging; that’s why I turn to #_## anykeyword###_ every time I need help moving company brooklyn

  9. Beulah Boyd表示:

    What an empowering experience it was attending the First Aid Course in Adelaide—I feel ready for anything now! CPR course

  10. Leroy Shelton表示:

    I’m curious about pricing for long distance movers in Sarasota—has anyone checked out cross country movers

  11. xnxx.com表示:

    Excellent post. I was checking constantly this blog and I am impressed!
    Extremely helpful info particularly the last part 🙂 I care
    for such information a lot. I was looking for this certain info for a long time.
    Thank you and best of luck.

  12. amiuksale.com表示:

    Some times its a pain in the ass to read what people wrote but this web site is really user genial!

  13. contact lens表示:

    Nice post. I learn something new and challenging on websites I stumbleupon everyday.
    It’s always helpful to read articles from other authors and
    practice something from other websites.

    Here is my web blog contact lens

  14. Hello, of course this post is actually fastidious and I have learned lot of things
    from it about blogging. thanks.

  15. Moving doesn’t need to be chaotic; just reach out to ####ANYKEYWORD### movers

  16. If some one wishes to be updated with most up-to-date
    technologies therefore he must be visit this website
    and be up to date all the time.

  17. Medicines prescribing information. Effects of Drug Abuse.
    where can i buy ceftin pill
    Actual about medicament. Read information now.

  18. SamuelBix表示:

    buy cipro online usa: CiPharmDelivery – buy ciprofloxacin over the counter

  19. Lydia Turner表示:

    Always check reviews before hiring a moving services —it saved me from a bad

  20. SamuelBix表示:

    amoxicillin canada price: amoxicillin 500mg without prescription – how much is amoxicillin prescription

  21. Myra Todd表示:

    Anyone else find packing up sentimental items difficult during their last big move? Let’s talk feelings over at long distance mover

  22. I’m thrilled that I chose reliablelocalmoversformyrecentmoveinStPetersburg; explorevaluableinformationaboutfindingmoversat### anykeyword### local moving companies st petersburg

  23. If you’re relocating soon long distance movers

  24. Shifting homes is tough but having great movers like ###ANYKEYWORD### makes such a difference! moving company brooklyn

  25. Mattie Ortega表示:

    I love the idea of DIY data recovery—it’s empowering! I’ve bookmarked your page, and I’m also exploring other resources like mac data recovery

  26. DanielAbice表示:

    https://prednibest.com/# prednisone 5 mg tablet without a prescription

  27. I like the helpful info you provide in your articles.

    I’ll bookmark your blog and check again here regularly.
    I’m quite certain I will learn many new stuff right here!
    Good luck for the next!

  28. Nikefut表示:

    dark websites darknet drugs dark web site

發佈留言

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