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,232,509 Responses

  1. Fastidious response in return of this matter with real arguments and
    describing everything regarding that.

  2. Hi there! I could have sworn I’ve been to this site before but after reading through
    some of the post I realized it’s new to me.
    Anyhow, I’m definitely delighted I found it and
    I’ll be book-marking and checking back often!

  3. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you!

    However, how can we communicate?

  4. streetstyle表示:

    I know this if off topic but I’m looking into starting my own weblog and was curious what all
    is needed to get set up? I’m assuming having a blog like yours
    would cost a pretty penny? I’m not very internet savvy so I’m not 100% sure.

    Any recommendations or advice would be greatly appreciated.

    Thanks

  5. My family always say that I am killing my time here at
    web, except I know I am getting familiarity everyday by
    reading thes nice content.

  6. My partner and I stumbled over here different web page and thought I may as well check things out.
    I like what I see so now i’m following you. Look forward
    to looking into your web page for a second time.

  7. It’s really very difficult in this busy life to listen news on Television, so I just use
    the web for that reason, and get the most up-to-date information.

  8. Dell表示:

    Good information. Lucky me I came across your blog by accident (stumbleupon).
    I have saved it for later!

  9. Hey just wanted to give you a quick heads
    up. The text in your content seem to be running off the screen in Firefox.
    I’m not sure if this is a formatting issue or something to do
    with internet browser compatibility but I thought I’d post
    to let you know. The style and design look great though! Hope you get the
    problem fixed soon. Thanks

  10. Way cool! Some very valid points! I appreciate you penning this article and the
    rest of the website is really good.

  11. Terrific article! That is the kind of info that are supposed to be shared across the internet.

    Shame on the seek engines for no longer positioning this put up upper!
    Come on over and seek advice from my site . Thanks =)

  12. I’m gone to tell my little brother, that he should also
    visit this webpage on regular basis to take updated from newest news update.

  13. Wow, superb blog format! How lengthy have you been running a blog for?
    you make running a blog look easy. The total look of your
    site is magnificent, let alone the content!

  14. Janette表示:

    What’s up to all, the contents existing at this site are actually
    amazing for people experience, well, keep up the good work fellows.

  15. I think the admin of this web site is genuinely working hard in favor of his web site, as here
    every data is quality based material.

  16. 7b6.cc表示:

    Hello! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing months
    of hard work due to no back up. Do you have any solutions to protect
    against hackers?

  17. Soila表示:

    This article is in fact a fastidious one it helps new internet people,
    who are wishing for blogging.

  18. No matter if some one searches for his necessary thing, thus he/she needs to be
    available that in detail, so that thing is maintained
    over here.

  19. Asking questions are actually nice thing if you are not understanding
    something completely, except this post gives fastidious understanding even.

  20. I read this paragraph completely concerning the resemblance of
    hottest and previous technologies, it’s amazing article.

  21. Please let me know if you’re looking for a writer for your site.
    You have some really good posts and I feel I would be a good asset.

    If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a
    link back to mine. Please shoot me an email if interested.
    Cheers!

  22. Hello, i believe that i saw you visited my weblog thus i got here to go back the
    choose?.I’m trying to in finding things to enhance my site!I suppose
    its good enough to make use of some of your concepts!!

  23. Hello, this weekend is pleasant designed for me, as this moment i am
    reading this enormous educational paragraph here
    at my residence.

  24. I was very happy to discover this great site. I wanted to thank you for ones time for this particularly fantastic read!!

    I definitely enjoyed every little bit of it and i also have you bookmarked to check out new information on your website.

  25. Heya terrific blog! Does running a blog similar to this take a lot of work?
    I have very little knowledge of computer programming but I had been hoping to start my own blog soon. Anyways,
    should you have any suggestions or tips for new blog owners please
    share. I understand this is off topic but I simply wanted to ask.
    Appreciate it!

  26. Your way of describing the whole thing in this paragraph
    is really pleasant, all be capable of effortlessly know it, Thanks a lot.

  27. lawtool.top表示:

    Fastidious answer back in return of this issue with genuine arguments and telling everything regarding that.

  28. Brodie表示:

    I’m very pleased to uncover this website. I need to to
    thank you for ones time for this fantastic read!!
    I definitely liked every part of it and i also have you book-marked to look
    at new things in your site.

  29. Hi there, just wanted to tell you, I liked this
    post. It was helpful. Keep on posting!

發佈留言

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