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,520,635 Responses

  1. Let me reveal you in my testimonial of NewULife.

    My web site ai detector turnitin check

  2. Way cool! Some very valid points! I appreciate you writing this post and the rest
    of the website is very good.

    Review my web site sexual assault lawyers philadelphia

  3. XYGENYX, a licensing firm ai detector turnitin for students
    FDA-registered items.

  4. Land 3 perk symbols to turn on the Pot Incentive
    Video Game.

    Feel free to surf to my web blog: online slots nz and online pokies nz

  5. ZenBusiness supplies the best membership options.

    Feel free to visit my web-site: how to start an llc in virginia

  6. Real HGH Perks (NewULife Hgh Gel Not Tested Yet).

    Visit my blog ai detector free grammarly

  7. A person necessarily assist to make critically posts I’d state.

    This is the very first time I frequented your website page and thus far?
    I surprised with the research you made to create this particular
    publish incredible. Wonderful job!

    Review my web-site; sexual assault lawyers philadelphia

  8. The best cbd drops for cats products are made from
    top notch hemp CBD.

  9. AhchcfzhwFef表示:

    dublin seo agency https://seo-agency-1.com/

  10. Just what I was looking for, appreciate it for posting.

    my blog post; sexual misconduct lawyers near Me

  11. I love it when folks come together and share thoughts. Great blog, continue
    the good work!

    Also visit my homepage … sexual assault defense lawyers near me

  12. If you are going for best contents like I do, simply go to see this web
    page every day since it presents feature contents, thanks

  13. NewULife Ranks. Welcome to my NewUlife Testimonial.

    Here is my blog post: heavy duty truck parts texas

  14. Let me reveal you in my review of NewULife.

    Check out my web blog; ai detector free

  15. NewULife Ranks. Invite to my NewUlife Review.

    Here is my web page: ai detector humanize

  16. Yes, Aviva mortgages over 70 are 1.97% APRC.

    Here is my blog … retirement interest-only mortgage rates uk

  17. AhchcfzhwFef表示:

    muslim seo agency https://seo-agency-1.com/

  18. An artificial USP human development hormone (somatropin).

    Feel free to visit my site; ai detector free online

  19. Lennyfut表示:

    dark web markets https://github.com/darkmarketlinkp22jr/darkmarketlink – darknet markets onion address

  20. Drugs information leaflet. What side effects can this medication cause?
    propranolol high pulse
    Some trends of drugs. Read now.

  21. Ozzyfut表示:

    dark web drug marketplace https://github.com/darknetdruglinksvojns/darknetdruglinks – dark market link

發佈留言

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