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,233,206 Responses

  1. A motivating discussion is definitely worth comment.

    I think that you should write more on this subject, it might
    not be a taboo matter but usually people do not discuss such subjects.
    To the next! Cheers!!

  2. I have been surfing online greater than three hours today, yet I
    never discovered any fascinating article like yours.
    It is beautiful worth sufficient for me. In my view, if all webmasters and bloggers made good content as you probably
    did, the internet might be much more useful than ever before.

  3. Hi it’s me, I am also visiting this web page regularly, this
    web site is genuinely good and the users are in fact sharing good
    thoughts.

  4. pltmbk.in.ua表示:

    This is a topic that is near to my heart… Cheers! Exactly where
    are your contact details though?

  5. This is a topic which is close to my heart… Many thanks!
    Where are your contact details though?

  6. Excellent blog! Do you have any tips and hints for aspiring writers?
    I’m planning to start my own site soon but I’m
    a little lost on everything. Would you advise starting with a free platform like WordPress or go for a
    paid option? There are so many choices out there that I’m completely confused ..
    Any ideas? Thanks!

  7. This piece of writing gives clear idea in favor of the
    new visitors of blogging, that truly how to do blogging.

  8. Hi there, all is going sound here and ofcourse every one is sharing information, that’s really fine, keep up writing.

  9. hi!,I love your writing so so much! proportion we keep up a correspondence extra approximately your article on AOL?
    I require a specialist on this area to resolve my problem.

    May be that’s you! Having a look forward to see you.

  10. ask.fm表示:

    Hello mates, its fantastic article about teachingand fully defined,
    keep it up all the time.

  11. Whoa! This blog looks exactly like my old one! It’s on a completely different topic but it has pretty much the same
    layout and design. Outstanding choice of colors!

  12. discuss表示:

    This is a topic which is close to my heart… Cheers! Exactly where are your contact details though?

  13. wonderful publish, very informative. I wonder why the opposite specialists of this sector do
    not realize this. You should proceed your writing.
    I am sure, you’ve a great readers’ base already!

  14. Its like you read my mind! You appear to know so much about this, like you wrote the book in it
    or something. I think that you could do with a few pics to drive the message home
    a little bit, but instead of that, this is magnificent blog.
    A fantastic read. I will definitely be back.

  15. Thanks for sharing your thoughts. I really
    appreciate your efforts and I am waiting for your next post thanks once again.

  16. We’re a gaggle of volunteers and opening a brand new scheme in our
    community. Your website offered us with useful info to work on. You’ve performed an impressive activity and our whole group shall
    be grateful to you.

  17. Hey there, I think your website might be having browser compatibility issues.
    When I look at your website in Chrome, it looks fine but when opening
    in Internet Explorer, it has some overlapping. I just wanted to give you a quick
    heads up! Other then that, excellent blog!

  18. Your way of describing everything in this post is in fact nice, every one can effortlessly know it, Thanks a lot.

  19. Way cool! Some very valid points! I appreciate you writing this post plus the rest of the site is really good.

  20. discuss表示:

    These are genuinely wonderful ideas in about blogging.
    You have touched some fastidious things here. Any way keep up
    wrinting.

  21. Awesome! Its genuinely amazing piece of writing, I have got much clear idea
    concerning from this post.

  22. Hi, Neat post. There is a problem together with your
    web site in internet explorer, might check this?
    IE nonetheless is the marketplace chief and a huge component
    to folks will pass over your fantastic writing due to this problem.

  23. This is a topic that’s near to my heart… Best
    wishes! Exactly where are your contact details though?

  24. We’re a group of volunteers and starting a new scheme in our community.
    Your web site offered us with helpful info to work
    on. You’ve performed an impressive task and our entire neighborhood will probably be thankful to you.

  25. Please let me know if you’re looking for a writer for your blog.

    You have some really good posts and I believe
    I would be a good asset. If you ever want
    to take some of the load off, I’d really like to write some articles for your blog in exchange for a
    link back to mine. Please shoot me an email if interested.

    Many thanks!

  26. Do you mind if I quote a couple of your articles
    as long as I provide credit and sources back to your website?

    My website is in the exact same niche as yours and my visitors would genuinely
    benefit from some of the information you provide here.

    Please let me know if this alright with you. Cheers!

  27. you’re truly a excellent webmaster. The website loading velocity is amazing.

    It sort of feels that you’re doing any distinctive trick.
    Also, The contents are masterwork. you’ve done a excellent task on this topic!

  28. I’m gone to tell my little brother, that he should also
    pay a quick visit this website on regular basis to obtain updated from most
    up-to-date information.

  29. This post will help the internet visitors for setting up new weblog
    or even a weblog from start to end.

發佈留言

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