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,228,294 Responses

  1. The 10 Most Scariest Things About Locksmith For Car Near Me Locksmith For Car Near Me

  2. Meds information sheet. Short-Term Effects.
    where can i get generic oxcarbazepine no prescription
    Actual about medicines. Read now.

  3. Drstobeese表示:

    griseofulvin online pharmacy: fincar pharmacy – viagra uk online pharmacy

  4. Ten Symptoms Of Anxiety Disorders Depression Myths That Aren’t Always The Truth symptoms of teenage depression

  5. 20 Reasons Why Bmw Spare Key Cost Will Never Be Forgotten Bmw
    Smart Key; https://Emplois.Fhpmco.Fr,

  6. Hmm it seems like your site ate my first comment (it was extremely long) so
    I guess I’ll just sum it up what I submitted
    and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to
    the whole thing. Do you have any suggestions for newbie blog writers?

    I’d genuinely appreciate it.

  7. Easydrorbix表示:

    buy online pharmacy: adipex online pharmacy diet pills – euro pharmacy cialis

  8. solar heater表示:

    It’s amazing in support of me to have a web site, which is valuable
    for my experience. thanks admin

  9. How do I copyright my site?. How do I use a pic from other sites, if I hapened 2b blogging on something relating to that pics?.

  10. Drstobeese表示:

    clindamycin target pharmacy: online pharmacies uk – guardian pharmacy loratadine

  11. Easydrorbix表示:

    can i buy viagra in a pharmacy: target pharmacy clomid price – kamagra 365 pharmacy

  12. I am really enjoying the theme/design of your site.

    Do you ever run into any internet browser compatibility issues?
    A handful of my blog visitors have complained about my site
    not operating correctly in Explorer but looks great in Firefox.
    Do you have any tips to help fix this problem?

  13. Moreover, crypto is decentralised with servers all over the world — there are over ten thousand
    nodes keeping track of all the transactions processed on the system.

  14. Easydrorbix表示:

    pharmacy pattaya kamagra: clomid online pharmacy no prescription – frys pharmacy

  15. Kevinamith表示:

    Клининговая компания уборка квартиры после пожара http://uborka-ot-pozhara.ru/

  16. What’s up all, here every person is sharing such familiarity, so it’s pleasant
    to read this webpage, and I used to pay a visit this website daily.

  17. Easydrorbix表示:

    best online pharmacy no prescription cialis: order xenical online pharmacy – anti fungal

  18. Laurene表示:

    My brother recommended I might like this website.

    He used to be totally right. This post truly made my day.
    You cann’t believe just how so much time I had spent for this information! Thank you!

  19. Heya i am for the first time here. I found this board and I to find It truly useful & it helped me out much.
    I hope to provide something back and help others like you helped
    me.

  20. sabai99表示:

    It’s actually a nice and helpful piece of info. I am satisfied that you shared this helpful information with us.
    Please keep us up to date like this. Thank you
    for sharing.

  21. Howdy are using WordPress for your site platform?
    I’m new to the blog world but I’m trying to get started and
    create my own. Do you require any html coding expertise
    to make your own blog? Any help would be greatly appreciated!

  22. 5 Killer Quora Answers To Private ADHD Assessment London Private adhd assessment London

  23. What Is The Reason Adding A Key Word To Your Life
    Will Make All The Impact 3 wheel scooters for sale
    near me (http://legendawiw.ru)

  24. I’m not that much of a internet reader to be honest but your sites
    really nice, keep it up! I’ll go ahead and bookmark your site to come back down the road.
    Many thanks

  25. Pretty section of content. I just stumbled upon your website and in accession capital
    to assert that I acquire in fact enjoyed account your blog posts.

    Anyway I’ll be subscribing to your augment and even I achievement you access
    consistently quickly.

  26. Three Reasons To Identify Why Your Robot Vacuum Isn’t Working (And
    What You Can Do To Fix It) Best mop and vacuum robot combo

  27. Easydrorbix表示:

    viagra us pharmacy online: mirtazapine pharmacy – ambien cr online pharmacy

  28. Drstobeese表示:

    rx discount pharmacy dade city fl: spain pharmacy viagra – mexican pharmacy adipex

  29. Easydrorbix表示:

    rx care pharmacy pearland tx: clobetasol propionate online pharmacy – lamotrigine pharmacy prices

發佈留言

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