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,123,152 Responses

  1. At this time it appears like Drupal is the best blogging platform out there right now.

    (from what I’ve read) Is that what you’re using on your blog?

  2. HorsePower Brands Omaha
    2525 N 117tһ Ave #300,
    Omaha, NE 68164, United States
    14029253112
    services industry home

  3. Hey this is kinda of off topic but I was wanting to know if blogs use
    WYSIWYG editors or if you have to manually code with HTML. I’m starting
    a blog soon but have no coding know-how so I wanted to get advice from someone with experience.
    Any help would be enormously appreciated!

  4. Drstobeese表示:

    safest online pharmacy viagra: euro pharmacy viagra – best drug store primer

  5. key表示:

    9 Things Your Parents Taught You About Key Fob Repair Service key

  6. What’s up to every body, it’s my first visit of this blog; this web site includes
    amazing and genuinely fine material in support of readers.

  7. Easydrorbix表示:

    Epivir-HBV: online pharmacy college – metoprolol people’s pharmacy

  8. 12 Companies Leading The Way In SEO Consultancy London seo Agency in london

  9. There’s certainly a lot to learn about this issue.
    I really like all the points you’ve made.

  10. It’s in reality a nice and useful piece of info. I’m glad that you simply shared this useful information with us.
    Please stay us informed like this. Thank you for sharing.

  11. Medicament information leaflet. Brand names.
    can i purchase cheap zofran pill
    Some information about medicine. Read now.

  12. Easydrorbix表示:

    warfarin monitoring pharmacy: online pharmacy delivery usa – mexico pharmacy adipex

  13. Drstobeese表示:

    best online pharmacy to buy viagra: detrol la online pharmacy – us viagra online pharmacy

  14. Easydrorbix表示:

    best pharmacy to buy provigil: meijer pharmacy amoxicillin – navarro discount pharmacy store locator

  15. Regards! Valuable stuff.

  16. Dorthy表示:

    10 ADHD Adults Test Tricks All Experts Recommend
    how to get tested for adhd (Dorthy)

  17. Drstobeese表示:

    brand viagra: uk pharmacy propecia – dostinex online pharmacy

  18. Easydrorbix表示:

    buy pharmacy: gabapentin online pharmacy – 365 pharmacy kamagra

  19. Undeniably believe that which you stated. Your favorite justification seemed to be
    on the web the easiest thing to be aware of. I say to
    you, I definitely get annoyed while people consider worries that they
    plainly do not know about. You managed to hit the nail upon the top
    and defined out the whole thing without having side effect , people can take a signal.
    Will likely be back to get more. Thanks

  20. spare Key Car表示:

    15 Surprising Stats About How Much Are Spare Car Keys spare Key Car

  21. konteyner表示:

    konteyner fiyatları | Yazınız, konuyu oldukça geniş bir perspektiften ele almış. Özellikle verdiğiniz stratejik tavsiyeler benim için çok değerliydi.

  22. Asking questions are really pleasant thing if you are
    not understanding anything entirely, however this post offers pleasant understanding even.

  23. Nicholas表示:

    Hi, I would like to subscribe for this web site to obtain hottest updates, so where can i do it please help out. https://gratisafhalen.be/author/columbustem/

  24. konteyner表示:

    konteyner fiyatları | Bu yazı, konuyu oldukça geniş bir perspektiften ele almış. Özellikle teknik detaylara verdiğiniz önem beni etkiledi.

  25. Easydrorbix表示:

    cipro online pharmacy: nexium 40 mg pharmacy – viagra professional online pharmacy

  26. Drstobeese表示:

    generic viagra online pharmacy no prescription: cialis online review online pharmacy – inhouse pharmacy baclofen

  27. Good post. I learn something totally new and challenging on websites I stumbleupon everyday.
    It will always be useful to read through articles from other writers and use a little something from their sites.

  28. Drstobeese表示:

    viagra pharmacy india: percocet overseas pharmacy – pharmacy discount

  29. What’s up it’s me, I am also visiting this site regularly, this
    web page is in fact good and the people are actually sharing good thoughts.

發佈留言

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