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,430,733 Responses

  1. Hi, I think your website might be having browser compatibility issues.

    When I look at your blog in Ie, it looks fine but when opening in Internet Explorer, it has
    some overlapping. I just wanted to give you a quick heads
    up! Other then that, amazing blog!

  2. %%

    My site :: vauxhall adam car key – https://www.spectshop.com

  3. Are you tired of dealing with the hassle of setting up a Cash App account?
    Look no further! Buy a verified Cash App account today and
    skip the verification process. Save time and enjoy the
    convenience of making fast and secure transactions. Don’t wait,
    get your verified Cash App account now!

  4. You can certainly see your enthusiasm in the work you write.
    The arena hopes for more passionate writers like you who
    are not afraid to say how they believe. At all times go after your heart.

  5. ducted ac表示:

    Excellent blog here! Also your web site loads up fast!
    What web host are you using? Can I get your affiliate link to
    your host? I wish my website loaded up as quickly as
    yours lol

  6. Effectively spoken genuinely! .

  7. Porn fans get a kick away of seeing a female pleasure herself. They especially want MILF’s to do it, like they’re obtaining a peek into something personal. It’s like watching fact TV – you can see what folks are doing in their own lives.

  8. Google表示:

    Everyone loves it when folks get together and share ideas.
    Great site, keep it up!

  9. growth matrix表示:

    Just desire to say your article is as astonishing.
    The clarity in your post is simply great and i could assume you are an expert on this subject.
    Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
    Thanks a million and please carry on the enjoyable work.

  10. programming表示:

    %%

    my web blog – programming

  11. What’s Going down i am new to this, I stumbled upon this I’ve discovered It absolutely helpful and
    it has helped me out loads. I’m hoping to give a contribution & assist other users like
    its helped me. Good job.

  12. Are you tired of going through lengthy verification processes?
    Look no further, as you can now buy a verified Cash App account hassle-free!
    Enjoy the convenience of instant peer-to-peer payments and seamless transactions without any verification delays.
    Don’t waste time, get your verified Cash App account today!

  13. Hello, i believe that i saw you visited my weblog so i got
    here to return the want?.I am attempting to in finding things
    to enhance my site!I assume its good enough to use some
    of your ideas!!

    my site :: top ten poker sites

  14. Audra表示:

    Thanks very interesting blog!

  15. I enjoy what you guys tend to be up too. Such clever work and exposure!
    Keep up the good works guys I’ve included you guys to our blogroll.

  16. %%

    Feel free to surf to my blog … suzuki alto key fob

  17. Marylou表示:

    %%

    My web page :: volvo key, Marylou,

  18. Just a smiling visitor here to share the love (:
    , btw outstanding layout.

    Also visit my homepage; find sex near me

  19. Syair SDY表示:

    Wow, awesome 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!

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

  21. teach表示:

    These are really enormous ideas in regarding blogging. You have touched some good
    points here. Any way keep up wrinting.

  22. Hi there I am so grateful I found your web site, I really found
    you by accident, while I was researching on Askjeeve
    for something else, Anyhow I am here now and would just like to say thanks a lot for a
    marvelous post and a all round enjoyable blog (I also love the
    theme/design), I don’t have time to read it all at the moment but I have book-marked it
    and also included your RSS feeds, so when I
    have time I will be back to read much more, Please do
    keep up the excellent work.

  23. Micki表示:

    %%

    my blog – smart car key replacement near me – Micki

  24. Right now it seems like WordPress is the preferred blogging platform available
    right now. (from what I’ve read) Is that what you are
    using on your blog?

發佈留言

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