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...

846,008 Responses

  1. It’s actually a great and useful piece of info. I’m happy that you simply shared this helpful information with us.
    Please stay us up to date like this. Thank you
    for sharing.

  2. I have read so many posts concerning the blogger
    lovers but this article is in fact a pleasant article, keep it up.

  3. Ahaa, its fastidious dialogue concerning this post here
    at this web site, I have read all that, so at this time me also commenting at this
    place.

  4. I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire
    someone to do it for you? Plz answer back as I’m looking
    to construct my own blog and would like to find out where u got this from.
    kudos

  5. Hey very nice blog!! Man .. Excellent ..
    Wonderful .. I will bookmark your blog and take the
    feeds additionally? I am satisfied to find numerous
    helpful info here within the post, we need work out extra strategies in this regard,
    thanks for sharing. . . . . .

  6. You have made some really good points there. I checked on the
    web for more information about the issue and found most people will go along with your views on this website.

  7. discuss表示:

    Howdy! This blog post couldn’t be written any better!
    Looking at this post reminds me of my previous roommate!
    He constantly kept preaching about this. I’ll forward this information to him.
    Pretty sure he’s going to have a great read.

    Many thanks for sharing!

  8. After going over a number of the blog articles on your blog, I truly like
    your way of writing a blog. I book marked it to my bookmark website list and will be checking back soon. Please check
    out my web site too and let me know your opinion.

  9. Oh my goodness! Amazing article dude! Many thanks, However
    I am going through issues with your RSS. I don’t know the
    reason why I cannot subscribe to it. Is there anybody
    else having identical RSS issues? Anyone who knows the
    answer will you kindly respond? Thanx!!

  10. The other day, while I was at work, my cousin stole my apple ipad and
    tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed and she has
    83 views. I know this is completely off topic but I had
    to share it with someone!

  11. katalog firm表示:

    Can you tell us more about this? I’d care to find out some additional
    information.

  12. www.4mark.net表示:

    Great work! That is the kind of info that should be shared across the web.
    Shame on the search engines for not positioning this post
    upper! Come on over and seek advice from my site .

    Thank you =)

  13. cutt.ly表示:

    There’s certainly a lot to learn about this topic.
    I like all the points you made.

  14. I used to be recommended this website via my cousin. I am no longer positive whether
    this submit is written by means of him as no one else recognize such certain about my trouble.
    You’re wonderful! Thanks!

  15. Hello my friend! I wish to say that this post is awesome, great written and include approximately all vital infos.

    I would like to peer extra posts like this .

  16. 포커캐슬表示:

    My coder is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the costs. But he’s tryiong none
    the less. I’ve been using WordPress on numerous websites for about a year and am concerned
    about switching to another platform. I have heard fantastic things about blogengine.net.

    Is there a way I can transfer all my wordpress posts into it?
    Any help would be greatly appreciated!

  17. With havin so much content do you ever run into any problems of
    plagorism or copyright violation? My site has a lot of unique content I’ve
    either written myself or outsourced but it seems a lot of it is popping it up all over the web without my authorization. Do you know
    any ways to help prevent content from being ripped off? I’d truly appreciate it.

  18. Have you ever thought about publishing an ebook or guest authoring on other websites?
    I have a blog based upon on the same topics you discuss and would love
    to have you share some stories/information. I know my audience
    would value your work. If you are even remotely interested,
    feel free to shoot me an email.

  19. Hi there! Someone in my Facebook group shared this site with us so I came to give it a look.
    I’m definitely enjoying the information. I’m bookmarking and
    will be tweeting this to my followers! Wonderful blog and outstanding design and style.

  20. I am sure this post has touched all the internet people,
    its really really pleasant paragraph on building up new blog.

  21. discuss表示:

    What’s up, I want to subscribe for this weblog to get latest updates, so where can i do it please help.

  22. Harrison表示:

    Hey I know this is off topic but I was wondering if you knew
    of any widgets I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping
    maybe you would have some experience with something
    like this. Please let me know if you run into anything. I truly enjoy
    reading your blog and I look forward to your new updates.

  23. www.fcc.gov表示:

    It’s actually a cool and helpful piece of information. I am
    satisfied that you simply shared this helpful information with us.
    Please stay us up to date like this. Thanks for sharing.

  24. If some one wishes expert view about blogging and site-building
    afterward i propose him/her to pay a visit this
    web site, Keep up the pleasant job.

  25. atavi.com表示:

    Heya i’m for the first time here. I came across this board and I find It really
    useful & it helped me out a lot. I hope to give something back and
    aid others like you aided me.

  26. Undeniably imagine that which you said. Your favourite reason appeared to be at the
    net the easiest thing to understand of. I say to you, I definitely get irked while folks consider worries that
    they just don’t recognise about. You managed to hit
    the nail upon the top as well as defined out the entire thing with no need
    side-effects , folks can take a signal. Will likely be back to get more.
    Thanks

  27. You need to be a part of a contest for one of the highest quality websites on the web.
    I will highly recommend this web site!

  28. Hello there, I found your blog by the use of Google while looking for a comparable topic, your
    website got here up, it seems great. I have bookmarked it in my google bookmarks.

    Hello there, just was aware of your weblog thru Google,
    and located that it is really informative. I’m going to watch out for brussels.
    I’ll be grateful if you happen to continue this in future.
    Many folks will likely be benefited out of your writing.

    Cheers!

  29. Keep this going please, great job!

  30. Hi there great website! Does running a blog similar to this take
    a large amount of work? I have virtually no understanding of computer programming however I was hoping to start my own blog in the near future.
    Anyway, should you have any recommendations or techniques for new blog owners
    please share. I know this is off topic but I just
    had to ask. Kudos!

發佈留言

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