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

886,123 Responses

  1. It is the best time to make some plans for the future and
    it’s time to be happy. I’ve read this post and if I could I
    desire to suggest you some interesting things or tips.
    Perhaps you can write next articles referring to this article.
    I desire to read even more things about it!

  2. Hi, I think your web site may be having web browser compatibility
    issues. Whenever I look at your website in Safari, it looks fine however when opening in Internet Explorer, it has some
    overlapping issues. I just wanted to give you a quick heads
    up! Aside from that, fantastic website!

  3. I like the valuable info you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I’m quite sure I will learn a lot of new stuff right here!
    Good luck for the next!

  4. Thank you for every other informative blog. Where else may just I am
    getting that type of information written in such an ideal manner?
    I have a undertaking that I’m just now running on, and I have been on the glance out for
    such information.

  5. It’s impressive that you are getting thoughts from this piece of writing as well as from our dialogue made at this time.

  6. Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something.
    I think that you can do with some pics to
    drive the message home a little bit, but other than that, this is excellent blog.

    A great read. I will definitely be back.

  7. Hey! Someone in my Myspace group shared this website with us so I
    came to give it a look. I’m definitely loving the information.
    I’m book-marking and will be tweeting this to my followers!

    Excellent blog and wonderful style and design.

  8. I take pleasure in, result in I found exactly what I was looking for.
    You have ended my 4 day lengthy hunt! God Bless
    you man. Have a great day. Bye

  9. help-sp.ru表示:

    It is not my first time to pay a quick visit this website, i am
    browsing this site dailly and get nice facts from here all
    the time.

  10. Attractive portion of content. I just stumbled upon your website and in accession capital to assert that
    I acquire actually enjoyed account your blog posts. Anyway I’ll be subscribing
    in your feeds or even I success you access constantly fast.

  11. I always used to read paragraph in news papers but now as I am a user of web therefore from now I am using net for posts, thanks
    to web.

  12. After looking at a handful of the blog posts on your website,
    I really appreciate your way of blogging. I saved it to my bookmark webpage list and will be checking back in the near future.

    Take a look at my web site as well and tell me what you think.

  13. Magnificent website. Plenty of useful info
    here. I’m sending it to some friends ans also sharing in delicious.

    And of course, thank you for your effort!

  14. Superb 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 more.
    Appreciate it!

  15. Admiring the hard work you put into your website and in depth information you offer.
    It’s great to come across a blog every once in a while
    that isn’t the same unwanted rehashed material. Great read!
    I’ve bookmarked your site and I’m adding your RSS feeds to my Google
    account.

  16. OPERATOTO表示:

    Fantastic web site. A lot of helpful info here. I’m sending it to a few pals ans additionally sharing in delicious.
    And of course, thanks for your effort!

  17. Wow that was unusual. I just wrote an really long comment but after I clicked submit
    my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyway, just
    wanted to say wonderful blog!

  18. Heya i am for the first time here. I found this
    board and I find It truly useful & it helped me out much.
    I hope to give something back and aid others like you helped me.

  19. Hello! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading
    through your posts. Can you suggest any other blogs/websites/forums
    that go over the same subjects? Thanks a ton!

  20. Way cool! Some extremely valid points! I appreciate
    you penning this article and the rest of the site is also really good.

  21. No matter if some one searches for his required thing, so he/she
    wishes to be available that in detail, therefore that thing is
    maintained over here.

  22. Sie haben ihren eigenen Pool. Unter Corona ist das verboten. Die Baustelle liegt auf Eis.
    Den kompletten Maßnahmenkatalog des Tourismusministeriums scheint noch keiner gesehen interessiert.
    Yesim Yalcin ist Ärztin in Antalya und macht nicht wenige Tage mit ihrem Bruder und seiner Frau hier Urlaub.
    Das Hotel bleibe diese Saison am besten zu. Normalerweise
    wird dann war’s das des Fastenmonats mit Familie und Freunden gefeiert.

    Nicht nur Sevki Erdogan hofft, dass viele Einschränkungen Anfang Juni fallen. Bis Mitte Juni warnt die Bundesregierung allerdings alle Deutschen, ins Ausland zu reisen. Um deutsche
    Urlauber trotz Corona wieder in die Türkei zu locken, holt die
    Regierung in Ankara den TÜV ins Boot. Momentan sind seine Hotelzimmer alle leer,
    nur schon ein paar der luxuriösen Villen sind belegt.
    Ein Hotelzimmer hätte sie allerdings nicht genommen.
    Die dürfen nun allerdings Staub ansetzen. Ferienhäuser werden diese Saison wohl insgesamt mehr gefragt
    sein. Schwimmen und Spazieren am Meer ist verboten. Die Türkei will, dass sie das bis zu den Sommerferien aufhebt.
    Der Küstenort Kas in der Türkei im Oktober 2019 – damals war die Welt noch frei von Corona.
    Bad unzerteilbar der Zimmer begutachtet. Im kleinen Boutique-Hotel
    fragen erst recht Merih Ciraks Stammgäste, wann sie diese Saison aufmacht.
    Der Hotelchef kann das in Rätseln sprechen “In Erscheinung treten einige Entscheidungen, die zu nichts gut sein”, sagt er.
    Ihr Vater bleibt dagegen dabei: Urlaub mit Corona mache keinen Spaß und sei gefährlich.
    Sicher ist das aber nicht.

  23. Your method of explaining all in this piece of writing is really nice, all
    can easily know it, Thanks a lot.

  24. hi!,I like your writing very a lot! percentage we communicate extra about
    your article on AOL? I require an expert on this house to resolve my problem.
    May be that’s you! Having a look forward to see you.

  25. naturally like your web-site but you need to check the spelling on quite a few
    of your posts. Many of them are rife with spelling issues and I in finding
    it very troublesome to tell the truth then again I will certainly come again again.

  26. Hi! This post couldn’t be written any better! Reading through this post reminds me
    of my good old room mate! He always kept chatting about this.
    I will forward this page to him. Fairly certain he will have a good read.
    Thank you for sharing!

  27. It is in reality a great and useful piece of information. I
    am glad that you simply shared this helpful information with us.
    Please keep us up to date like this. Thank you for sharing.

  28. master jitu表示:

    Hello there, just became alert to your blog
    through Google, and found that it’s truly informative.
    I am going to watch out for brussels. I’ll be grateful if you continue this in future.

    Lots of people will be benefited from your writing.
    Cheers!

  29. 500px.com表示:

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

發佈留言

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