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

913,686 Responses

  1. Hayley表示:

    Thanks for ones marvelous posting! I definitely enjoyed reading it, you could be a great author.
    I will make certain to bookmark your blog and will often come back later in life.
    I want to encourage you continue your great work, have a nice morning!

  2. Attractive section of content. I just stumbled upon your blog and in accession capital to assert that I get in fact enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  3. Wonderful goods from you, man. I’ve understand your stuff
    previous to and you’re just too great. I really like what you’ve acquired here, really like
    what you’re saying and the way in which you say it.

    You make it enjoyable and you still take care of to keep it wise.

    I cant wait to read far more from you. This is really
    a terrific web site.

  4. I like the valuable info you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I’m quite certain I will learn plenty of new stuff right here!
    Best of luck for the next!

  5. What i do not understood is if truth be told how you’re now not actually a lot more neatly-favored
    than you might be right now. You’re very intelligent.

    You already know thus significantly when it comes to this
    topic, made me individually consider it from numerous various angles.
    Its like women and men are not involved until it
    is one thing to accomplish with Lady gaga!
    Your own stuffs nice. At all times maintain it up!

  6. Wow, superb blog format! How lengthy have you been running a blog for?
    you made blogging look easy. The whole glance of your web site is fantastic, let alone the content
    material!

  7. Good post. I learn something new and challenging on blogs
    I stumbleupon every day. It’s always useful to read articles from other writers
    and practice a little something from their sites.

  8. When someone writes an paragraph he/she maintains the plan of a user in his/her
    brain that how a user can know it. Therefore that’s why this
    article is amazing. Thanks!

  9. Wonderful work! This is the type of info that
    are meant to be shared around the net. Disgrace on the search engines
    for not positioning this submit higher! Come
    on over and discuss with my web site . Thank you =)

  10. Hi it’s me, I am also visiting this site regularly, this web site is genuinely pleasant and the people are in fact sharing fastidious thoughts.

  11. We absolutely love your blog and find a lot of your post’s
    to be just what I’m looking for. can you offer
    guest writers to write content for you? I wouldn’t mind publishing
    a post or elaborating on a number of the subjects you write in relation to here.

    Again, awesome web site!

  12. It’s in fact very difficult in this full of activity life
    to listen news on TV, so I simply use internet for
    that reason, and get the most recent news.

  13. I truly love your website.. Great colors & theme.
    Did you develop this amazing site yourself? Please reply
    back as I’m wanting to create my own website and would love to learn where you
    got this from or exactly what the theme is named. Cheers!

  14. bdproshno.com表示:

    I blog often and I genuinely appreciate your information. The article has truly peaked my interest.
    I will take a note of your site and keep checking for new information about once a week.
    I opted in for your RSS feed as well.

  15. My brother recommended I may like this web site. He was entirely right.
    This put up actually made my day. You cann’t consider just how much time I had spent for this information! Thank you!

  16. Hi, i believe that i saw you visited my website thus i got here to return the desire?.I’m trying to to find things to enhance my website!I suppose its adequate to make use of a few of your concepts!!

  17. Julio表示:

    Wow, this post is good, my younger sister is analyzing such things, so I am going to
    tell her.

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

  19. Bill表示:

    Hey there would you mind sharing which blog platform you’re using?
    I’m looking to start my own blog in the near future but I’m having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design seems different then most blogs and I’m
    looking for something completely unique. P.S My apologies for
    getting off-topic but I had to ask!

  20. Hollis表示:

    I love what you guys are up too. This type of clever work and reporting!
    Keep up the amazing works guys I’ve included you guys to
    blogroll.

  21. What’s up, of course this piece of writing is genuinely nice
    and I have learned lot of things from it on the topic
    of blogging. thanks.

  22. Hi there this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.

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

  23. discuss表示:

    We stumbled over here by a different web address and thought I
    may as well check things out. I like what I
    see so i am just following you. Look forward to looking into your web page repeatedly.

  24. Undeniably believe that which you stated. Your favorite justification appeared to be on the
    web the easiest thing to be aware of. I say to you, I certainly get irked while people think about worries
    that they just don’t know about. You managed to hit the nail upon the top as
    well as defined out the whole thing without having side-effects , people could take a signal.
    Will likely be back to get more. Thanks

  25. It is perfect time to make some plans for the long run and it’s time to be happy.
    I have read this publish and if I could I wish to suggest you some attention-grabbing issues or advice.

    Maybe you can write subsequent articles referring to this article.
    I want to read even more things approximately it!

  26. Very nice post. I simply stumbled upon your blog and
    wished to mention that I’ve really loved surfing around your blog posts.

    In any case I will be subscribing for your rss feed and I’m hoping you write again very soon!

  27. Hi! I know this is somewhat off-topic however I needed to ask.
    Does operating a well-established blog such as yours take
    a large amount of work? I’m completely new to operating a blog however I do write in my diary everyday.
    I’d like to start a blog so I can share my own experience and feelings online.

    Please let me know if you have any ideas or tips for
    new aspiring bloggers. Appreciate it!

  28. I do accept as true with all of the ideas you’ve offered in your post.
    They’re very convincing and can certainly work. Nonetheless, the posts are very brief for
    starters. May you please lengthen them a bit from next time?
    Thank you for the post.

  29. Gretchen表示:

    Right away I am ready to do my breakfast, later than having my breakfast coming
    again to read other news.

發佈留言

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