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,231,872 Responses

  1. It’s appropriate time to make a few plans for the longer term and it’s time to be happy.
    I have read this publish and if I could I wish to suggest you few attention-grabbing things or suggestions.
    Perhaps you could write subsequent articles
    regarding this article. I want to learn more things about it!

  2. Fantastic goods from you, man. I’ve remember your stuff prior to and you are just extremely magnificent.
    I actually like what you have received here, certainly like what you’re saying and the
    way in which through which you say it. You are making it entertaining and you still
    care for to keep it wise. I can’t wait to learn much more from you.

    That is actually a wonderful website.

  3. This article is really a pleasant one it assists new net
    visitors, who are wishing in favor of blogging.

  4. I’d like to thank you for the efforts you have
    put in penning this website. I am hoping to check out the same high-grade blog posts
    from you later on as well. In truth, your creative
    writing abilities has motivated me to get my
    own, personal blog now 😉

  5. Hello there! I could have sworn I’ve been to this site before
    but after browsing through some of the post I realized it’s new to
    me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and
    checking back often!

  6. lyqaq.top表示:

    Outstanding post however I was wanting to know if you could write a litte more on this topic?
    I’d be very grateful if you could elaborate a little bit further.
    Thanks!

  7. It’s going to be ending of mine day, however before finish I am
    reading this enormous piece of writing to increase my experience.

  8. My brother suggested I might like this website.
    He was totally right. This post actually made my day.

    You cann’t imagine just how much time I had spent
    for this info! Thanks!

  9. Wow, that’s what I was searching for, what a information! existing here at this website, thanks admin of this web page.

  10. Hello it’s me, I am also visiting this web site regularly,
    this site is truly good and the visitors are really sharing nice thoughts.

  11. I love what you guys are usually up too. Such clever work and reporting!
    Keep up the superb works guys I’ve you guys to our blogroll.

  12. Thanks on your marvelous posting! I genuinely enjoyed reading it, you can be a great author.
    I will be sure to bookmark your blog and will come back later on. I want to encourage you to ultimately continue your great job, have a nice evening!

  13. peatix.com表示:

    I blog often and I seriously thank you for your content.

    This article has really peaked my interest. I will bookmark your site and keep checking for
    new details about once per week. I subscribed to your
    Feed as well.

  14. What’s up to all, the contents existing at this website are in fact remarkable for people knowledge, well, keep up the
    nice work fellows.

  15. Tremendous issues here. I’m very glad to see your post.
    Thanks a lot and I’m having a look forward to contact you.

    Will you please drop me a mail?

  16. Good post. I learn something totally new and challenging on blogs
    I stumbleupon everyday. It will always be exciting to read through articles from other authors
    and use something from other websites.

  17. If some one wants expert view on the topic of blogging then i
    advise him/her to pay a quick visit this web site, Keep up the fastidious job.

  18. Greetings! Quick question that’s completely off topic. Do you know how to make your site mobile
    friendly? My site looks weird when viewing from my iphone4.
    I’m trying to find a theme or plugin that might be able
    to resolve this issue. If you have any recommendations, please share.
    Appreciate it!

  19. cqms.skku.edu表示:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make
    your point. You definitely know what youre talking about, why throw away your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?

  20. It’s actually a nice and helpful piece of information. I am
    glad that you just shared this useful info with us.
    Please stay us up to date like this. Thanks for sharing.

  21. Ahaa, its good dialogue concerning this post
    here at this weblog, I have read all that, so now me also commenting here.

  22. Hello very nice web site!! Man .. Excellent .. Amazing ..
    I’ll bookmark your website and take the feeds also? I’m satisfied to
    seek out numerous useful information here within the submit, we’d like develop more strategies on this regard, thank you
    for sharing. . . . . .

  23. Wonderful article! This is the kind of information that should be
    shared around the net. Disgrace on Google for not positioning this post higher!
    Come on over and visit my web site . Thank you =)

  24. Great post. I was checking constantly this blog and I’m impressed!
    Very useful information specifically the last part :
    ) I care for such information much. I was seeking
    this particular info for a long time. Thank you and best of luck.

  25. Hello mates, good post and pleasant urging commented here,
    I am genuinely enjoying by these.

  26. I have been surfing online more than 3 hours today, yet I never
    found any interesting article like yours. It is pretty worth enough for me.
    In my opinion, if all website owners and bloggers made good content as
    you did, the internet will be a lot more useful than ever before.

  27. Fantastic beat ! I wish to apprentice while you amend your site, how can i subscribe for a blog site?

    The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea

  28. I have to thank you for the efforts you’ve put in penning this site.
    I’m hoping to check out the same high-grade blog posts by you
    in the future as well. In truth, your creative
    writing abilities has inspired me to get my very own site now 😉

發佈留言

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