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

911,002 Responses

  1. SUPERSLOT表示:

    Wow, great article post.Really thank you! Fantastic.

  2. What’s up, I wish for to subscribe for this blog to get newest updates, thus where can i do it please help.

  3. First off I want to say excellent blog! I had
    a quick question in which I’d like to ask if you do not mind.

    I was curious to know how you center yourself and clear your mind prior to writing.
    I have had a tough time clearing my thoughts in getting my thoughts out there.
    I do enjoy writing but it just seems like the first 10 to 15 minutes are
    lost just trying to figure out how to begin. Any ideas or tips?
    Thanks!

  4. You made some decent points there. I looked on the web for more info about the issue and found most individuals will go along with your views on this site.

  5. I’m really impressed with your writing skills and also with the layout on your blog.

    Is this a paid theme or did you modify it yourself? Anyway keep up the nice quality writing, it’s rare to see a
    great blog like this one today.

  6. Every weekend i used to pay a quick visit this website, for the
    reason that i wish for enjoyment, since this this site conations genuinely pleasant funny data too.

  7. When some one searches for his essential thing, thus he/she
    needs to be available that in detail, therefore that thing is
    maintained over here.

  8. Its not my first time to visit this site, i am visiting this web site dailly and get good information from here all
    the time.

  9. auto parts,表示:

    Hey There. I found your blog using msn. This is a really well
    written article. I’ll make sure to bookmark it and come back to read more of
    your useful information. Thanks for the post.
    I’ll definitely comeback.

  10. I’m truly enjoying the design and layout of your site.
    It’s a very easy on the eyes which makes it much more pleasant for
    me to come here and visit more often. Did you hire out a developer to create your theme?

    Excellent work!

  11. Hello, I enjoy reading through your article. I like to write a
    little comment to support you.

  12. An impressive share! I’ve just forwarded this onto a coworker who has been doing a little research on this.
    And he in fact ordered me lunch because I stumbled upon it for
    him… lol. So let me reword this…. Thank YOU for the meal!!

    But yeah, thanx for spending time to discuss this issue here on your blog.

  13. Hello there! Do you use Twitter? I’d like to follow you if that would be okay.
    I’m definitely enjoying your blog and look forward to new posts.

  14. Greetings! Very useful advice in this particular post!

    It’s the little changes that make the biggest changes.
    Many thanks for sharing!

  15. betot.ru表示:

    When some one searches for his vital thing, so he/she wants to be available that in detail,
    thus that thing is maintained over here.

  16. Logan表示:

    Way cool! Some extremely valid points! I appreciate you penning
    this post plus the rest of the site is also very good.

  17. Gilbert表示:

    I?m not tһat much oof a online reader to bе honest but yοur sites really nice, keep it up!
    I’ll go ahhead аnd bookmark y᧐ur site tߋ comе Ƅack lаter.

    Μany thanks

  18. Very great post. I simply stumbled upon your weblog and wanted to mention that
    I’ve truly enjoyed surfing around your blog posts.
    After all I’ll be subscribing on your rss feed and I am hoping you write
    again very soon!

  19. I am genuinely delighted to glance at this webpage posts which consists of tons of useful data, thanks for providing such data.

  20. It’s going to be ending of mine day, however before end I am reading this
    fantastic paragraph to increase my knowledge.

  21. discuss表示:

    Pretty great post. I simply stumbled upon your blog and wanted
    to say that I have truly loved browsing your weblog posts.
    After all I’ll be subscribing for your feed and
    I am hoping you write once more very soon!

  22. What’s Happening i am new to this, I stumbled upon this I have found It
    absolutely useful and it has aided me out
    loads. I hope to give a contribution & help other users like its aided me.
    Great job.

  23. I have been browsing online more than three hours today,
    yet I never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you
    did, the net will be a lot more useful than ever before.

  24. I go to see daily a few blogs and websites to read posts, however this web site gives quality based writing.

  25. Plus, you can play options like Frequent Draw Roulette, Vegas Roulette, and Zoon Roulette.

    Also vsit my blkog :: 카지노사이트추천

  26. Slot Bank Neo表示:

    Incredible points. Outstanding arguments. Keep up the amazing work.

  27. dating chat表示:

    naturally like your web-site howevesr you have to test the slelling on several of yoour posts.

    A number of them are rife with spelling issues and I in finding it very troublesome
    to tell the truth however I’ll surely come again again.

  28. Hi there, after reading this amazing piece of writing i am too cheerful to share my
    knowledge here with friends.

  29. I really like reading an article that will make people think.
    Also, thank you for allowing me to comment!

  30. Hello, I read your new stuff like every week. Your humoristic style
    is awesome, keep up the good work!

發佈留言

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