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

830,717 Responses

  1. When I originally commented I seem to have clicked the -Notify me when new comments
    are added- checkbox and now every time a comment is added I recieve 4 emails with the same comment.

    Is there a way you are able to remove me from that service?
    Thanks!

  2. wxjfdb.com表示:

    This is a topic that’s near to my heart… Cheers! Where are your contact details though?

  3. fensterguru表示:

    Da sich Einbrecher den Zugang meist durch ein Kellerfenster oder Erdgeschossfenster verschaffen, sollten Sie
    an zusätzliche Sicherheitsmaßnahmen denken. Bei einem
    frei zugänglichen Kellerfenster kann neben anderen Maßnahmen z.

    B. auch ein zusätzliches Fenstergitter angebracht werden. Auch Lichtschächte können effektiv gegen das Aufhebeln gesichert werden. Möchten Sie im Kellerwohnraum zusätzlich Ihre Privatsphäre schützen,
    können Sie das mit einer Ornamentverglasung oder einer nachträglich angebrachten Folie erreichen. Die Abmessungen eines Fensters bestimmen, ob es
    zweiflügelig sein kann und darf. Entstehen zwar keine nach DIN festgelegten Doppelflügelfenster Maße,
    dennoch gibt es durchaus verschiedene typisierte Fenster mit
    Richtmaßen, die sich an der Mauerwerks-DIN orientieren. Etwa kann ein Fenster mit einer
    Breite oder Höhe von 40 cm keine zwei Fensterflügel haben.
    Nach DIN sind auch die Mindestgröße für Rettungswege durch Fensteröffnungen und die Mindestbelichtungsfläche von Räumen, die für den Daueraufenthalt von Menschen bestimmt sind,
    festgelegt. Entsprechend Material können Doppelfenster nicht nur in verschiedenen Breiten und Höhen, sondern auch in unterschiedlichen Einbautiefen gekauft werden.

  4. An impressive share! I’ve just forwarded this onto a coworker who had been doing a little homework on this.
    And he in fact ordered me lunch simply because I stumbled upon it for him…
    lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanks for spending time to talk about this topic here on your site.

  5. Can you tell us more about this? I’d want to
    find out more details.

  6. Hey this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.

    I’m starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

  7. I really like what you guys are up too. This sort of clever
    work and exposure! Keep up the amazing works guys I’ve incorporated you guys
    to blogroll.

  8. discuss表示:

    What’s Happening i’m new to this, I stumbled upon this
    I have found It positively useful and it has aided
    me out loads. I hope to contribute & assist other users like its aided
    me. Great job.

  9. I’m not sure where you are getting your info, but good topic.
    I needs to spend some time learning much more or understanding more.
    Thanks for fantastic info I was looking for this information for my mission.

  10. Very nice post. I just stumbled upon your blog and
    wished to say that I’ve really enjoyed surfing around your
    blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!

  11. Its such as you learn my thoughts! You seem to grasp a lot approximately this,
    such as you wrote the guide in it or something.

    I feel that you just could do with a few % to pressure the message home a little bit, however instead of that, this is fantastic blog.
    A fantastic read. I’ll definitely be back.

  12. I savor, cause I discovered exactly what I was having a look for.
    You’ve ended my 4 day lengthy hunt! God Bless you man. Have
    a nice day. Bye

  13. Greetings! Very helpful advice within this post! It is the little changes which will make
    the biggest changes. Many thanks for sharing!

  14. If you desire to grow your knowledge only keep visiting this website and be
    updated with the most recent news update posted here.

  15. I was recommended this web site by means of my cousin. I am not sure whether this post is written by
    means of him as nobody else recognise such unique about my trouble.
    You are amazing! Thank you!

  16. discuss表示:

    Right now it appears like Movable Type is the best blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?

  17. Hi mates, how is everything, and what you wish
    for to say concerning this post, in my view its in fact awesome
    in favor of me.

  18. Very nice post. I simply stumbled upon your weblog and wished
    to say that I have really loved surfing around your weblog posts.
    In any case I’ll be subscribing on your rss feed and I am hoping
    you write again soon!

  19. blip.fm表示:

    At this moment I am going to do my breakfast,
    after having my breakfast coming over again to read further news.

  20. Hi there just wanted to give you a quick heads up. The text in your article seem
    to be running off the screen in Internet explorer. 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 solved soon. Many thanks

  21. I absolutely love your site.. Excellent colors & theme. Did you create this amazing site yourself?
    Please reply back as I’m wanting to create my own personal blog
    and would love to find out where you got this from or what the theme is named.
    Many thanks!

  22. certainly like your web site however you have to check the spelling on quite
    a few of your posts. Several of them are rife with spelling issues and I to find it very bothersome to tell
    the reality then again I will certainly come again again.

  23. Wonderful blog! I found it while browsing 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!
    Thank you

  24. I quite like reading through a post that can make people think.
    Also, many thanks for allowing for me to comment!

  25. Good blog you have here.. It’s hard to find high-quality writing like yours nowadays.
    I really appreciate people like you! Take care!!

  26. I’m not sure exactly why but this weblog is loading incredibly slow for me.
    Is anyone else having this issue or is it a problem on my end?

    I’ll check back later on and see if the problem still
    exists.

  27. Hey! Would you mind if I share your blog with my facebook group?
    There’s a lot of folks that I think would really appreciate your content.
    Please let me know. Many thanks

  28. Hi there mates, fastidious post and fastidious urging commented here, I am truly enjoying by these.

  29. It is the best time to make some plans for the long run and it’s time to be happy.
    I have learn this post and if I could I desire to recommend you few interesting things or tips.
    Perhaps you could write next articles regarding this article.
    I desire to learn even more issues approximately it!

  30. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using WordPress on a variety of websites for about a year and am nervous
    about switching to another platform. I have heard fantastic things about blogengine.net.
    Is there a way I can import all my wordpress content into it?
    Any kind of help would be really appreciated!

發佈留言

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