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,247,111 Responses

  1. BernardMop表示:

    medication zestoretic Lisinopril 1st cheapest Lisinopril

  2. Hello to every single one, it’s truly a pleasant for me to go to see this web site, it includes helpful Information.

  3. Redevelopments of commercial Complex, Mall, Officies or Shop or Condo

  4. лечение наркозависимости в стационаре gov.ukrbb.net/viewtopic.php?f=3&t=6377 .

  5. DarrenUnjuh表示:

    http://iverfast.com/# ivermectin india

  6. Wow, that’s what I was searching for, what a information!
    existing here at this weblog, thanks admin of this web page.

  7. Oil Palm plantation still in demand

  8. DarrenUnjuh表示:

    https://plavixclo.com/# buy clopidogrel bisulfate

  9. Nikefut表示:

    deep web drug markets free dark web free dark web

  10. BrandenSlomy表示:

    buy prednisone canadian pharmacy: raypharm – 10 mg prednisone tablets

  11. My spouse and I stumbled over here different website and thought I might check things out.

    I like what I see so i am just following you. Look forward to looking over
    your web page for a second time.

    Check out my page; รับจัดงานแต่งงาน

  12. Faith Kanner表示:

    Oil Palm plantation still in demand

  13. This is the right web site for anybody who really wants
    to understand this topic. You understand
    a whole lot its almost tough to argue with you (not that
    I actually will need to…HaHa). You definitely put a new
    spin on a topic that has been written about for ages.
    Great stuff, just wonderful!

  14. BrandenSlomy表示:

    clomid medication: clomid rex pharm – how to get cheap clomid without insurance

  15. Bobofng.Com表示:

    7 Things About Realistic Sex Dolls You’ll Kick Yourself For
    Not Knowing silicone realistic sex dolls (Bobofng.Com)

  16. id=”firstHeading” class=”firstHeading mw-first-heading”>Search гesults

    Help

    English

    Tools

    Tools
    mοve to sidebar hide

    Actions

    Ԍeneral

    Here іs myy blog post … ร้านดอกไม้บรรยากาศอบอุ่น

  17. DarrenUnjuh表示:

    https://iverfast.com/# buy ivermectin for humans uk

  18. BernardMop表示:

    plavix medication PlavixClo Best Price plavix best price

  19. Our ranking is based on security, game selection, bonuses,
    and user experience.

  20. Hi there, I believe your website could possibly
    be having web browser compatibility problems. When I take a look at your web site in Safari,
    it looks fine however, if opening in Internet Explorer, it’s got
    some overlapping issues. I merely wanted to provide you with a quick heads up!
    Apart from that, excellent site!

  21. hey there and thank you for your information – I’ve definitely picked up something new
    from right here. I did however expertise
    a few technical points using this web site, as I experienced to reload the site lots of times
    previous to I could get it to load correctly. I had been wondering if your hosting is OK?
    Not that I’m complaining, but sluggish loading instances times will very frequently affect
    your placement in google and could damage your high-quality score if advertising
    and marketing with Adwords. Well I’m adding this RSS to my
    email and could look out for a lot more of your respective
    fascinating content. Make sure you update this again very soon.

  22. Reina Alescio表示:

    Converting Land to plantation

  23. Nikefut表示:

    tor market links deep web drug links dark web drug marketplace

  24. Good day! This is my first visit to your blog!
    We are a collection of volunteers and starting a new initiative in a community
    in the same niche. Your blog provided us
    beneficial information to work on. You have done a marvellous job!

發佈留言

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