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,683,365 Responses

  1. discuss表示:

    Ԝhat’s up mates, how is the whole thing, and what yoᥙ deѕire to say regarding this p᧐st, in my view іts actually awesome
    in support of me.

    my blⲣog :: discuss

  2. Wow, superb blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your web site is
    magnificent, as well as the content!

  3. https://bogin94.z19.web.core.windows.net/research/bogin94-(76).html
    A twinset can have a “fuddy duddy” status, however it undoubtedly doesn’t should look quaint.

  4. Devon Kempton表示:

    https://bogin79.z12.web.core.windows.net/research/bogin79-(35).html
    Whether you may have your heart on embroidery, embellishment, sequin, or ruched silk smoothness, golden dresses look great in all types and designs.

  5. Internet表示:

    continuously i used to read smaller articles that as well clear
    their motive, and that is also happening with this
    paragraph which I am reading now.

  6. April表示:

    For newest information you have to pay a visit world-wide-web and on the web I found this site as a finest site for
    most up-to-date updates.

  7. raja99表示:

    I feel that is among the so much significant information for me.

    And i am glad studying your article. But want to observation on some common issues, The website taste is perfect,
    the articles is in point of fact excellent : D. Good job, cheers

  8. Have you ever thought about publishing an e-book or guest authoring on other sites?
    I have a blog based upon on the same topics you discuss and would really like to have you share
    some stories/information. I know my viewers would value your work.
    If you’re even remotely interested, feel free to send me
    an email.

  9. Piece of writing writing is also a fun, if you be familiar with
    after that you can write otherwise it is difficult to write.

  10. Stormy表示:

    Great post.

    Heгe is my site :: slot (Stormy)

  11. I like the valuable information you provide to
    your articles. I’ll bookmark your blog and take a look at again here frequently.
    I am rather certain I will learn many new stuff right right here!
    Good luck for the next!

  12. Bennie表示:

    І ҝnow tһis if off topic Ьut Ӏ’m looking
    into starting my own weblog and wɑs curious what ɑll is required tо get setup?
    I’m assuming һaving a blog like ʏοurs woᥙld
    cost a pretty penny? I’m not νery web smart sօ Ӏ’m not
    100% sᥙre. Any tips or advice woսld bе
    gгeatly appreciated. Αppreciate it

    Here is my web рage lotto – Bennie

  13. Nudesville表示:

    If you wish for to improve your familiarity only
    keep visiting this website and be updated with the
    most recent news update posted here.

  14. Since the admin of this website is working, no hesitation very soon it
    will be famous, due to its feature contents.

    Here is my homepage :: binary options

  15. ансвер表示:

    If you want to take a good deal from this article then you have to apply
    such techniques to your won web site.

  16. porn review表示:

    Generally I don’t read post on blogs, but I wish to say that this write-up
    very forced me to take a look at and do so! Your writing taste has been surprised me.
    Thank you, quite great article.

  17. Bettina表示:

    asbestos mesothelioma legal claim – Bettina,

  18. Your means of describing everything in this post
    is in fact good, all be capable of effortlessly understand it, Thanks a lot https://www.lescaledetente.com/massage-suedois/

  19. taxi Airdrie表示:

    When someone writes an paragraph he/she keeps the image of a user
    in his/her brain that how a user can understand it. Therefore that’s why this
    post is great. Thanks!

  20. Lena表示:

    railroad cancer lawsuit (Lena)

  21. Maricruz表示:

    how to file a mesothelioma claim (Maricruz)

  22. Bird guru表示:

    Hurrah, that’s what I was exploring for, what a material!

    present here at this website, thanks admin of this site.

  23. mesothelioma Attorney wisconsin – yogaasanas.Science,

  24. I will right away seize your rss as I can’t to find your email subscription link or newsletter service.

    Do you’ve any? Kindly permit me realize so that I could
    subscribe. Thanks.

    Visit my blog Eco-Friendly Tiny House

  1. 2023-12-04

    […] yߋur live cam present to hundreds of strangers. Below аre 12 of the hottest ԝebsites for bazongas stay cam ladies, exploгed in detail. We assess what they provide, ɑnd bomɗ the way theу […]

發佈留言

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