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,305 Responses

  1. Drstobeese表示:

    scripts rx pharmacy: Detrol – xenical dubai pharmacy

  2. Guide To 3 Wheeler Pushchairs: The Intermediate
    Guide On 3 Wheeler Pushchairs 3 wheeler Pushchair

  3. We invite you to explore and contribute to the growth of this ecosystem.

  4. Drstobeese表示:

    cialis singapore pharmacy: mexican online pharmacy reviews – hy-vee pharmacy

  5. The 9 Things Your Parents Taught You About Pvc Door Hinges pvc Door hinges

  6. A Step-By Step Guide For Choosing Your Treehouse Canopy Treehouse Bunkbed

  7. Blake表示:

    Are You Getting The Most From Your Mini Chest Freezer Uk?
    chest freezer suitable for garage (Blake)

  8. I was curious if you ever considered changing the layout of your blog?
    Its very well written; I love what youve got to say. But maybe you could a little more in the way of
    content so people could connect with it better. Youve got an awful lot of text for
    only having one or 2 images. Maybe you could space it
    out better?

  9. Easydrorbix表示:

    good online pharmacy: australian pharmacy online – do pharmacy sell viagra

  10. 14 Misconceptions Common To Best Truck Accident Lawyer truck accident lawyer near me

  11. konteyner表示:

    konteyner | Bu konuyu bu kadar iyi açıklayan bir yazı bulmak zor. Tebrikler!

  12. vegan food表示:

    Thanks for the good writeup. It in truth was once a enjoyment account it.

    Glance advanced to far brought agreeable from you!

    By the way, how can we keep in touch?

  13. Drstobeese表示:

    buy amoxicillin no prescription fda checked pharmacy: express pharmacy – family rx pharmacy

  14. Hi there! Someone in my Facebook group shared this website with us so
    I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to
    my followers! Superb blog and brilliant design.

  15. 5 Locked Keys In Car Service Lessons From The Pros
    vehicle unlocking

  16. ads508表示:

    First off I would like to say awesome blog! I had a quick question that I’d like to ask if you do not
    mind. I was interested to find out how you center yourself and clear
    your head prior to writing. I’ve had a hard time clearing my thoughts in getting my ideas out there.

    I truly do take pleasure in writing however it just
    seems like the first 10 to 15 minutes are wasted simply
    just trying to figure out how to begin. Any recommendations or hints?
    Many thanks!

  17. Easydrorbix表示:

    clindamycin target pharmacy: tylenol pharmacy scholarship – pharmacy store locator

  18. Hiya very cool blog!! Guy .. Excellent .. Superb .. I’ll
    bookmark your blog and take the feeds also? I’m
    glad to search out so many helpful information right here within the put up, we want develop extra techniques in this regard, thanks for sharing.
    . . . . .

  19. We are a group of volunteers and opening a new scheme in our community.
    Your web site provided us with valuable information to work on. You’ve done an impressive job and our whole community
    will be grateful to you.

  20. Активные игроки в покер получают шансы кликнуть на главное событие 2021 года
    – https://kzpokerdomplus.com/
    МЕ spf autumn!

  21. You Are Responsible For An Adhd Private Assessment Budget?
    12 Tips On How To Spend Your Money how do i Get an adhd assessment

  22. konteyner表示:

    konteyner fiyatları | Bu makale, konuya dair her türlü soruma yanıt verdi. Özellikle teknik detaylara verdiğiniz önem beni etkiledi.

  23. When someone writes an piece of writing he/she keeps the idea of a user in his/her brain that how a user can be aware of it.
    So that’s why this post is outstdanding. Thanks!

  24. Easydrorbix表示:

    Hyzaar: ambien india pharmacy – mail order pharmacy no prescription

  25. Drstobeese表示:

    express rx pharmacy services: meds online – buy nolvadex online pharmacy

  26. Easydrorbix表示:

    asacol online pharmacy: mexican pharmacy weight loss – giant food store pharmacy hours

  27. Awesome! Its really amazing paragraph, I have got much clear idea about from this paragraph.

  28. The 10 Scariest Things About Composite Door Repair Near
    Me composite door Repair near me

  29. Drstobeese表示:

    Starlix: target store pharmacy hours – heb pharmacy online

發佈留言

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