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,247,630 Responses

  1. What’s up everyone, it’s my first go to see at this website,
    and paragraph is truly fruitful for me, keep up posting these posts.

  2. I simply could not depart your site before suggesting that I extremely enjoyed the usual info a person provide to your guests?
    Is gonna be again frequently to check out new posts

  3. Nice post. I was checking continuously this blog and I am impressed!
    Very helpful info specially the last part 🙂 I care for such info much.
    I was looking for this certain information for a very long time.
    Thank you and best of luck.

  4. Slot Bank Neo表示:

    excellent publish, very informative. I ponder why
    the other experts of this sector do not notice this. You must continue your writing.

    I am sure, you’ve a great readers’ base already!

  5. Hey there just wanted to give you a quick heads up
    and let you know a few of the images aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two different
    web browsers and both show the same outcome.

  6. discuss表示:

    Ridiculous story there. What happened after? Good luck!

  7. Very good information. Lucky me I recently found your website by accident (stumbleupon).

    I’ve saved as a favorite for later!

  8. Nice answers in return of this question with real
    arguments and explaining the whole thing on the topic of that.

  9. Hi there, You’ve done a great job. I’ll definitely digg it and personally suggest to my friends.

    I am sure they will be benefited from this web site.

  10. mnasaty.net表示:

    Hello, of course this post is actually nice and I have learned lot of things from it concerning blogging.
    thanks.

  11. necessary review. At the same time visit my own,
    personal, personal site to see agen slot deposit pulsa.
    This amazing web site has received diagnosis from the slot online games women and men in Indonesia.
    Right now there are actually plenty of exciting promos because you
    join today.

  12. Good day! I could have sworn I’ve been to this website
    before but after checking through some of the post I realized it’s new to me.

    Anyhow, I’m definitely delighted I found it and I’ll be bookmarking and
    checking back frequently!

  13. webcamera.ru表示:

    This paragraph provides clear idea in favor of the new people of
    blogging, that actually how to do blogging and site-building.

  14. Fantastic blog! Do you have any recommendations for aspiring writers?
    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you recommend 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 recommendations? Many thanks!

  15. discuss表示:

    Hello my family member! I wish to say that this article is
    amazing, nice written and include almost all vital infos.

    I would like to see extra posts like this .

  16. discuss表示:

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

  17. madu samawa表示:

    My spouse and I stumbled over here coming from a different web address and thought
    I should check things out. I like what I see so now i am following you.
    Look forward to checking out your web page again.

  18. Hey there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly?
    My site looks weird when browsing from my iphone 4.
    I’m trying to find a theme or plugin that might be able to fix
    this problem. If you have any suggestions, please share.
    With thanks!

  19. This is very interesting, You’re a very
    skilled blogger. I’ve joined your rss feed and look forward
    to seeking more of your wonderful post. Also, I have
    shared your site in my social networks!

  20. Hi there! I just would like to offer you a big thumbs up for the great info you have got
    right here on this post. I am coming back to your
    web site for more soon.

  21. This is the right webpage for anyone who would like to understand this topic.
    You know so much its almost tough to argue with you (not that I
    really would want to…HaHa). You definitely put a fresh spin on a topic which has been written about for ages.
    Excellent stuff, just wonderful!

  22. lhb1.com表示:

    Interesting blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple adjustements would really
    make my blog shine. Please let me know where you got your design. Cheers

  23. We’re a group of volunteers and opening a brand new scheme
    in our community. Your site provided us with useful information to work on. You’ve done a formidable process
    and our entire community can be grateful to you.

  24. Very good post! We will be linking to this particularly great article on our
    site. Keep up the great writing.

  25. tiktok表示:

    Howdy! This blog post could not be written any better!
    Looking through this article reminds me of my previous roommate!
    He constantly kept talking about this. I most certainly will forward this information to him.
    Fairly certain he’ll have a great read. Many thanks for sharing!

  26. It’s very straightforward to find out any topic on net as compared to textbooks,
    as I found this piece of writing at this web page.

  27. In Italien werden die Einschränkungen Abschnittweise zurückgenommen.
    Einen Spanien-Urlaub an der Zeit sein für den Frühsommer
    bis jetzt nicht buchen. Buffets in Hotels sind
    verboten. Buchungen für den Zugang zu Strandbädern sollen verhindern, dass die
    Menschen zu dicht aneinander sonnenbeschienen braten. Mallorca und die
    anderen Urlaubsinseln können aber darauf hoffen, vielleicht doch ein bisschen früher eine beschränkte Zahl von in- und ausländischen Besuchern empfangen zu dürfen. Desinfektionsmittel müssen überall bereit stehen.
    Es gebe eine Arbeitsgruppe, die über ein solches Pilotprojekt spreche, dabei man erste Erfahrungen sammeln wolle, bestätigte Verkehrsminister José Luis Ábalos.
    Die Regierung in Madrid bekräftigte, dass man frühestens ab Ende
    Juni mit einer weitgehenden Grenzöffnung für Touristen rechnen könne.
    Selbst den Spaniern werde es bis nachher verboten bleiben,
    in andere Regionen des Landes zu reisen. Die Inseln setzen sich schon lange für die Errichtung eines “sicheren Tourismuskorridors”.
    Die Menschen, die trotz geschlossener Grenzen einreisen dürfen,
    weil sie in Spanien eine Erstwohnung haben oder eine Arbeitsstelle antreten,
    müssen zwei Wochen ohne.

  28. Its like you read my mind! You appear to know a lot about this,
    like you wrote the book in it or something. I think that you
    can do with a few pics to drive the message home a little bit, but
    other than that, this is fantastic blog. A great read. I will definitely be back.

發佈留言

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