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,231,666 Responses

  1. I’m truly enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often.
    Did you hire out a developer to create your theme?

    Fantastic work!

  2. I am really happy to read this website posts which carries lots of
    useful facts, thanks for providing these information.

  3. Flor表示:

    Every weekend i used to pay a visit this web page, as i wish for enjoyment, for
    the reason that this this website conations in fact pleasant
    funny material too.

  4. Spot on with this write-up, I really believe this web site needs much more attention. I’ll probably
    be returning to read more, thanks for the advice!

  5. Connie表示:

    Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your
    web site is magnificent, let alone the content!

  6. It’s great that you are getting thoughts from this piece of
    writing as well as from our argument made at this place.

  7. I visited several blogs except the audio quality for audio songs present at this web site
    is really excellent.

  8. ask.fm表示:

    Howdy, i read your blog occasionally and i own a similar one and i
    was just wondering if you get a lot of spam responses? If so how
    do you prevent it, any plugin or anything you can advise? I get so much lately it’s driving me
    crazy so any assistance is very much appreciated.

  9. Greetings, There’s no doubt that your site might be having internet browser compatibility
    issues. When I take a look at your web site in Safari, it looks fine however, if opening in Internet Explorer, it has some overlapping issues.
    I simply wanted to provide you with a quick heads up! Aside from that, wonderful website!

  10. Very good blog! Do you have any tips and hints for aspiring writers?
    I’m hoping to start my own website soon but I’m a little lost on everything.

    Would you propose starting with a free platform like WordPress or go for a paid
    option? There are so many choices out there that I’m totally confused ..

    Any recommendations? Kudos!

  11. Gordon表示:

    Keep on working, great job!

  12. Thanks , I’ve recently been searching for information about this subject for ages and yours is the greatest I have discovered so far.
    But, what about the conclusion? Are you positive concerning the supply?

  13. Latashia表示:

    I enjoy what you guys are usually up too. This kind of clever work and coverage!

    Keep up the wonderful works guys I’ve incorporated you guys to my own blogroll.

  14. I have read so many articles or reviews on the topic of the blogger lovers but this article is really a good post, keep it up.

  15. discuss表示:

    For the reason that the admin of this site is working, no uncertainty very rapidly it will be
    renowned, due to its feature contents.

  16. Its such as you read my mind! You appear to grasp a lot about this, like you wrote the
    e-book in it or something. I feel that you simply can do with some p.c.
    to power the message house a bit, however other than that,
    that is magnificent blog. A fantastic read.
    I will definitely be back.

  17. Does your website have a contact page? I’m having problems locating it but,
    I’d like to shoot you an email. I’ve got some ideas for
    your blog you might be interested in hearing.
    Either way, great website and I look forward to seeing it develop over
    time.

  18. Johnnie表示:

    What’s up to every one, it’s in fact a pleasant for
    me to visit this web site, it consists of helpful
    Information.

  19. Hey! Do you use Twitter? I’d like to follow you if that
    would be ok. I’m undoubtedly enjoying your blog and look forward to new posts.

  20. Clyde表示:

    Your style is so unique in comparison to other folks I have read stuff
    from. I appreciate you for posting when you have the opportunity, Guess I will just book mark
    this web site.

  21. sc.sie.gov.hk表示:

    Wow, incredible weblog format! How lengthy have you ever been blogging for?
    you made running a blog glance easy. The whole look of your
    website is great, let alone the content material!

  22. What’s Happening i’m new to this, I stumbled upon this I’ve found It absolutely helpful and it has aided me
    out loads. I am hoping to contribute & aid different users like its helped
    me. Good job.

  23. I do not even know how I stopped up right here, however I assumed this post was once good.

    I don’t realize who you are but definitely you are going to a well-known blogger for
    those who are not already. Cheers!

  24. Excellent article. I’m experiencing some of these issues as
    well..

  25. Its like you read my mind! You appear to know so much about this,
    like you wrote the book in it or something. I think that you could do with a
    few pics to drive the message home a little bit, but other than that, this is wonderful blog.
    An excellent read. I’ll definitely be back.

  26. Nice blog right here! Also your site quite a bit up fast!
    What web host are you the usage of? Can I am getting your affiliate link to your host?
    I desire my web site loaded up as quickly as yours lol

  27. I’m not that much of a online reader to be honest but your blogs really nice, keep it up!
    I’ll go ahead and bookmark your website to come back later.
    Many thanks

  28. Fantastic goods from you, man. I have understand your stuff previous to
    and you’re just extremely excellent. I actually like what you have
    acquired here, really like what you’re stating and the way in which you
    say it. You make it entertaining and you still take care of to keep it sensible.
    I cant wait to read far more from you. This is really a wonderful site.

  29. I really like looking through an article that can make people think.
    Also, many thanks for permitting me to comment!

  30. Jeffrey表示:

    Pretty portion of content. I simply stumbled upon your web site and in accession capital to claim that I acquire in fact enjoyed account
    your weblog posts. Any way I’ll be subscribing on your feeds or even I success you get admission to consistently rapidly.

發佈留言

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