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,414,209 Responses

  1. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you! However, how could we communicate?

    My homepage :: online poker ranking

  2. I really like your blog.. very nice colors & theme.
    Did you make this website yourself or did you hire someone to
    do it for you? Plz answer back as I’m looking to create my own blog
    and would like to know where u got this from.
    kudos

  3. Rodrigo表示:

    %%

    my site – suzuki swift key replacement (Rodrigo)

  4. Florrie表示:

    ilford door panels – Florrie

  5. Good day! I simply would like to offer you a huge thumbs
    up for your excellent information you’ve got here on this post.

    I am coming back to your blog for more soon.

    Also visit my homepage; online poker ranking

  6. I like the valuable info you provide in your articles.
    I’ll bookmark your weblog and check again here regularly.
    I’m quite sure I’ll learn many new stuff right here! Best of
    luck for the next!

  7. Dannielle表示:

    %%

    my webpage – renault trafic key fob (Dannielle)

  8. Mariel表示:

    %%

    my website … nissan qashqai key replacement (Mariel)

  9. What’s up mates, fastidious article and pleasant arguments commented here,
    I am genuinely enjoying by these.

  10. te.Legra.Ph表示:

    door fitter reading; te.Legra.Ph,

  11. If some one desires to be updated with hottest technologies then he must
    be pay a visit this web site and be up to date all the time.

  12. lana rhoades表示:

    Hey there! I know this is kinda off topic however ,
    I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa?
    My site discusses a lot of the same topics as yours and I feel we could greatly
    benefit from each other. If you are interested feel free to send me an e-mail.
    I look forward to hearing from you! Terrific blog by the
    way!

  13. Hi friends, how is all, and what you wish for to say regarding this post, in my view
    its really amazing in favor of me.

  14. question表示:

    It is really a nice and helpful piece of info.
    I’m glad that you just shared this helpful info with us.
    Please stay us informed like this. Thank you for sharing.

  15. s666表示:

    Hi there I am so grateful I found your blog, I
    really found you by accident, while I was searching on Google for something else, Regardless I
    am here now and would just like to say kudos
    for a tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t
    have time to go through it all at the minute but I have
    bookmarked it and also added in your RSS feeds, so when I have time
    I will be back to read much more, Please do keep up the fantastic jo.

  16. Hi there! This is my first visit to your blog! We
    are a collection of volunteers and starting a new initiative in a community in the same
    niche. Your blog provided us valuable information to work on. You have done a wonderful job!

  17. What’s up everybody, here every person is sharing these know-how, so it’s nice
    to read this weblog, and I used to pay a quick visit this blog every day.

  18. Great beat ! I would like to apprentice while you amend your website, how can i subscribe for a weblog site?

    The account aided me a appropriate deal.

    I had been tiny bit acquainted of this your broadcast provided vivid clear idea

  19. likbezz.ru表示:

    Ten Stereotypes About Coffee Bean Machine That Aren’t Always True

    Also visit my homepage :: beans machines (likbezz.ru)

  20. Hi there! Someone in my Myspace group shared this website with us so I came to look
    it over. I’m definitely enjoying the information. I’m book-marking
    and will be tweeting this to my followers! Superb blog and brilliant design and style.

  21. Awesome! Its truly remarkable post, I have got much clear idea regarding from this post.

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

  23. Selma表示:

    asbestos attorney asbestos [Selma]

  24. Nicolas表示:

    %%

    my blog post … boating accidents lawyer near me (Nicolas)

發佈留言

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