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

842,126 Responses

  1. Hello There. I found your blog using msn. This is a really well written article.

    I’ll make sure to bookmark it and return to read more of your useful info.
    Thanks for the post. I will definitely comeback.

  2. It’s great that you are getting thoughts from this paragraph
    as well as from our dialogue made here.

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

  4. I am sure this post has touched all the internet users,
    its really really pleasant article on building up new website.

  5. You ought to be a part of a contest for one of the greatest websites on the net.
    I am going to recommend this blog!

  6. Lilian表示:

    When I initially commented I appear to have clicked the -Notify me when new comments are
    added- checkbox and from now on every time a comment is added
    I receive 4 emails with the same comment. Perhaps there
    is a means you can remove me from that service?
    Thanks!

  7. Useful information. Lucky me I found your web site by accident, and I am shocked
    why this coincidence did not came about in advance!
    I bookmarked it.

  8. telegra.ph表示:

    If you would like to take much from this paragraph then you
    have to apply these strategies to your won website.

  9. Hello to all, how is the whole thing, I think every one is getting more from this web
    site, and your views are nice for new users.

  10. Yes! Finally someone writes about new york.

  11. What’s up to all, it’s in fact a nice for me to pay a
    quick visit this web page, it contains important Information.

  12. I was suggested this web site via my cousin. I’m not positive whether this post
    is written by way of him as no one else know
    such distinctive approximately my difficulty.
    You’re wonderful! Thanks!

  13. Hello There. I found your weblog the use of msn. This is a
    very smartly written article. I’ll make sure to bookmark it and return to learn extra of your useful info.
    Thanks for the post. I will certainly comeback.

  14. Heya! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I ended up losing months of hard
    work due to no backup. Do you have any methods to stop hackers?

  15. Good day! I know this is somewhat off topic but I was wondering if you knew where I
    could find a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having
    problems finding one? Thanks a lot!

  16. atavi.com表示:

    This website definitely has all the information and facts I wanted about this subject and didn’t
    know who to ask.

  17. Magnificent goods from you, man. I’ve understand your
    stuff previous to and you’re just extremely wonderful. I actually like what you’ve acquired here, certainly like what
    you are stating and the way in which you say it.
    You make it entertaining and you still take care of to keep it sensible.
    I can not wait to read far more from you. This is actually a great site.

  18. dx.v68.tw表示:

    Oh my goodness! Impressive article dude! Many thanks, However I am experiencing problems
    with your RSS. I don’t know the reason why I cannot join it.
    Is there anybody having identical RSS issues?
    Anybody who knows the answer can you kindly respond? Thanks!!

  19. I’m more than happy to uncover this web site. I wanted to thank you for your time for this particularly wonderful read!!
    I definitely enjoyed every little bit of it and I have you saved as a favorite to see new
    things on your site.

  20. Way cool! Some very valid points! I appreciate you writing this write-up and also the rest
    of the website is also really good.

  21. clean表示:

    Howdy! This is my 1st comment here so I just wanted
    to give a quick shout out and tell you I really enjoy reading your articles.
    Can you suggest any other blogs/websites/forums that cover the same
    topics? Thank you!

  22. I pay a quick visit day-to-day some blogs and sites
    to read articles or reviews, but this web site
    provides feature based writing.

  23. Admiring the commitment you put into your website and in depth
    information you provide. It’s good to come across a blog every once in a while that isn’t the same outdated rehashed
    material. Great read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  24. IPTV Panel表示:

    Thanks for your personal marvelous posting! I quite enjoyed reading
    it, you are a great author. I will be sure to bookmark your blog and will eventually come back at some point.
    I want to encourage you to definitely continue your
    great writing, have a nice evening!

  25. Have you ever thought about including a little bit more than just your articles?
    I mean, what you say is fundamental and everything. But think of if you
    added some great photos or video clips to give your posts more, “pop”!
    Your content is excellent but with images and video clips, this website could certainly be
    one of the very best in its niche. Good blog!

  26. I’m gone to inform my little brother, that he should
    also go to see this weblog on regular basis to get updated from
    most up-to-date information.

  27. important review. As well visit by myself, personal site which can be played slot deposit pulsa tanpa potongan.
    That incredible web page has received name caused by slot deposit pulsa gambling establishment
    contributing factors in Indonesia. In that respect there are usually a
    few beautiful promos when you join at this point.

發佈留言

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