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,890 Responses

  1. Saved as a favorite, I like your web site!

  2. I am curious to find out what blog platform
    you have been working with? I’m experiencing some small security
    issues with my latest site and I’d like to find something more safeguarded.
    Do you have any suggestions?

  3. I am curious to find out what blog system you
    have been using? I’m experiencing some minor security problems with my latest
    site and I would like to find something more safeguarded.
    Do you have any recommendations?

  4. jyzlyjlb.com表示:

    Having read this I thought it was really informative. I appreciate you spending some time and energy to put this informative article together.
    I once again find myself spending way too much time both reading
    and commenting. But so what, it was still worthwhile!

  5. discuss表示:

    Way cool! Some extremely valid points! I appreciate you writing this write-up plus the rest of
    the site is also really good.

  6. There is definately a lot to learn about this subject.
    I love all of the points you made.

  7. Sweet blog! I found it while searching on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!

    Many thanks

  8. I always used to read article in news papers but now as I am a user of web therefore from now I am using net for
    posts, thanks to web.

  9. madusamawa表示:

    These are in fact fantastic ideas in concerning blogging. You have touched some fastidious
    things here. Any way keep up wrinting.

  10. After I originally left a comment I appear to have clicked on the -Notify
    me when new comments are added- checkbox and now whenever a comment is added I recieve four emails with the
    exact same comment. Is there a means you are able to remove me from that
    service? Many thanks!

  11. I’ve been exploring for a bit for any high-quality
    articles or weblog posts in this kind of space .
    Exploring in Yahoo I eventually stumbled upon this website.
    Studying this info So i am happy to exhibit that I have a very excellent uncanny feeling
    I found out just what I needed. I so much surely will make certain to don?t disregard this web site
    and give it a look on a relentless basis.

  12. Do you have any video of that? I’d want to find out more details.

  13. Ahaa, its good dialogue regarding this article at this place at this website, I have read all that,
    so at this time me also commenting at this place.

  14. Hi! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing several weeks of hard work
    due to no backup. Do you have any solutions to prevent hackers?

  15. Wow, this post is fastidious, my sister is analyzing these things, so I am going to let know her.

  16. Hi there, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam responses?
    If so how do you stop it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any help is
    very much appreciated.

  17. You should take part in a contest for one of the finest websites on the internet.
    I am going to recommend this blog!

  18. atavi.com表示:

    Thank you for some other wonderful post. The place else may just
    anybody get that kind of info in such a perfect approach of writing?
    I’ve a presentation next week, and I am at the look for such information.

  19. Definitely believe that which you said. Your
    favorite reason appeared to be on the net the easiest
    thing to be aware of. I say to you, I definitely get irked while people think about worries that they just do not know
    about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal.
    Will probably be back to get more. Thanks

  20. It’s hard to find well-informed people about this topic, but you sound like you know what you’re talking about!
    Thanks

  21. I’m extremely impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you customize
    it yourself? Anyway keep up the excellent quality
    writing, it is rare to see a nice blog like this one these days.

  22. 500px.com表示:

    Excellent, what a webpage it is! This website provides valuable facts to us,
    keep it up.

  23. Nice post. I learn something new and challenging
    on sites I stumbleupon on a daily basis. It’s always helpful to read through content from other writers and use something
    from other sites.

  24. Very good blog post. I definitely appreciate this site.
    Continue the good work!

  25. Thanks for your marvelous posting! I truly enjoyed reading it, you might be a great author.I will be sure to
    bookmark your blog and will often come back down the road.
    I want to encourage that you continue your great job,
    have a nice morning!

  26. brewwiki.win表示:

    Great article.

  27. Do you mind if I quote a few of your posts as
    long as I provide credit and sources back to your website?
    My blog site is in the exact same area of interest
    as yours and my visitors would truly benefit from a lot of the information you
    provide here. Please let me know if this ok with
    you. Appreciate it!

  28. cutt.ly表示:

    Thanks for the auspicious writeup. It in reality was once a amusement account it.
    Glance complicated to more delivered agreeable
    from you! However, how could we keep up a correspondence?

  29. Excellent blog you have here but I was wanting to know if you knew of any user discussion forums
    that cover the same topics discussed in this article?
    I’d really love to be a part of community where I can get comments from
    other experienced individuals that share the same interest.
    If you have any suggestions, please let me know. Cheers!

  30. Hi there, I believe your site could possibly be having
    internet browser compatibility problems. When I look at your website in Safari,
    it looks fine however when opening in IE, it’s got some overlapping issues.

    I merely wanted to give you a quick heads up! Other
    than that, great site!

發佈留言

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