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,234,484 Responses

  1. This web site certainly has all of the info I wanted concerning
    this subject and didn’t know who to ask.

  2. This post is worth everyone’s attention. When can I find out more?

  3. uz-gis.in.ua表示:

    I have read so many content concerning the blogger lovers except this piece of writing is really a
    good post, keep it up.

  4. Link exchange is nothing else however it is simply placing the other person’s website link on your page
    at appropriate place and other person will also do similar in favor of you.

  5. Rueben表示:

    Pretty! This has been a really wonderful post. Many
    thanks for supplying these details.

  6. Thank you for the auspicious writeup. It in fact was a amusement account
    it. Look advanced to far added agreeable from you! By the way,
    how could we communicate?

  7. First off I want to say great blog! I had a quick question which I’d like to ask
    if you don’t mind. I was interested to know how you
    center yourself and clear your mind prior to writing.
    I’ve had a hard time clearing my mind in getting my thoughts
    out there. I truly do enjoy writing however it just seems like the first 10
    to 15 minutes are generally wasted simply just trying to figure out how to begin.
    Any ideas or hints? Many thanks!

  8. Useful info. Lucky me I discovered your site by chance, and I’m shocked
    why this coincidence didn’t came about earlier! I bookmarked it.

  9. Hello would you mind stating which blog platform you’re using?
    I’m looking to start my own blog soon but I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and
    Drupal. The reason I ask is because your design and
    style seems different then most blogs and I’m looking for something unique.
    P.S Apologies for being off-topic but I had to ask!

  10. An impressive share! I have just forwarded this onto a friend who was
    conducting a little homework on this. And he in fact ordered
    me breakfast simply because I stumbled upon it for him…
    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending some time to discuss this issue here on your
    website.

  11. It’s a shame you don’t have a donate button! I’d without a doubt donate to this superb blog!
    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to new updates and will share this website with
    my Facebook group. Chat soon!

  12. Appreciating the time and effort you put into your website and in depth information you provide.
    It’s great to come across a blog every once in a while that isn’t the same unwanted rehashed information.
    Wonderful read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  13. Hi there mates, its great piece of writing on the topic
    of teachingand fully defined, keep it up all the time.

  14. Thank you a lot for sharing this with all folks you actually understand what you are speaking approximately!

    Bookmarked. Kindly additionally talk over with my web site =).

    We could have a link change arrangement among us

  15. It’s very easy to find out any topic on web
    as compared to textbooks, as I found this paragraph at this web page.

  16. Hi there, all the time i used to check web site posts here early in the daylight,
    as i enjoy to learn more and more.

  17. What’s up it’s me, I am also visiting this website daily, this website is really
    pleasant and the visitors are actually sharing nice thoughts.

  18. This is the right web site for anyone who would like to understand this topic.
    You know so much its almost hard to argue with you (not that I really will need to…HaHa).
    You definitely put a new spin on a subject which has been written about for years.
    Wonderful stuff, just excellent!

  19. Touche. Outstanding arguments. Keep up the good work.

  20. That is a good tip particularly to those fresh to the blogosphere.
    Simple but very precise info… Appreciate your sharing
    this one. A must read post!

  21. We’re a group of volunteers and starting a new scheme in our community.
    Your web site provided us with valuable information to work on. You have done a formidable job and our whole community will be grateful to you.

  22. When I initially commented I clicked the “Notify me when new comments are added” checkbox
    and now each time a comment is added I get three emails with the same comment.
    Is there any way you can remove me from that service?

    Bless you!

  23. Very good post. I’m going through some of these issues as well..

  24. I’ve been browsing on-line greater than 3 hours nowadays, yet I never discovered any fascinating article like yours.
    It is pretty price sufficient for me. In my opinion, if all web owners
    and bloggers made good content material as you probably did,
    the net might be much more helpful than ever before.

  25. I love what you guys tend to be up too. This kind of clever
    work and exposure! Keep up the great works guys I’ve included you guys
    to my own blogroll.

  26. I was able to find good advice from your blog articles.

  27. Yes! Finally someone writes about bacgiang.tcvn.gov.vn.

  28. I like the helpful information you provide for your articles.

    I’ll bookmark your blog and take a look at once more here regularly.

    I am fairly sure I will learn many new stuff proper right here!

    Good luck for the following!

  29. Hey there superb website! Does running a blog like this take a great
    deal of work? I have absolutely no expertise in coding but I had been hoping to start my
    own blog in the near future. Anyhow, if you have any ideas
    or techniques for new blog owners please share.
    I know this is off topic however I just wanted to ask.
    Thanks a lot!

發佈留言

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