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,229,766 Responses

  1. Having read this I thought it was extremely informative. I appreciate you finding the time and energy to put this content together.
    I once again find myself spending a significant amount of
    time both reading and commenting. But so what, it was still
    worthwhile!

  2. BIKE-Redakteur Ludwig verzieht die Mundwinkel, als er die Lampe vergeben und vergessen nimmt.
    Damit soll er Biken gehen? Genau, wir wollten wissen: Wie viel Licht braucht man zum sportlichen Biken wirklich?
    Müssen es die Highend Strahler mit externem Akku sein, oder tut es auch die günstige Kompaktklasse mit integrierter Energiequelle?
    Für die Leistungsklasse mit externem Akku haben wir aber auch
    nicht in die oberste Preiskategorie gegriffen: Als Limit haben wir 200 Euro gesetzt und dafür mindestens 1000
    Lumen gefordert. Ab diesem Wert liefern die Leuchten auch für sportlichste Einsätze erfahrungsgemäß genügend
    Power. Die kompakten Modelle in unserem Test sind so klein und leicht, dass sie permanent im Rucksack liegen dürfen und somit auf jeder Tour parat sind, falls
    es mal wieder später wird. Die Frage war also nicht nur: Wie groß sind die Unterschiede zwischen Kompakt- und Leistungsklasse?

    Sondern auch: Können die günstigen Modelle der Leistungsklasse der rund doppelt so
    teuren Edelkonkurrenz mithalten?

  3. I all the time emailed this blog post page to all my friends, because if like to read it afterward
    my links will too.

  4. Quality articles is the secret to invite the users to go to see
    the site, that’s what this web site is providing.

  5. Your way of telling all in this piece of writing is in fact pleasant, every one be capable of
    without difficulty be aware of it, Thanks a lot.

  6. If you desire to obtain a good deal from this article then you have
    to apply these methods to your won blog.

  7. That is very attention-grabbing, You’re an overly professional blogger.
    I’ve joined your rss feed and look ahead to looking for more of your wonderful post.
    Additionally, I have shared your site in my social networks

  8. 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? Excellent work!

  9. When someone writes an piece of writing he/she maintains the thought of
    a user in his/her brain that how a user can know it.
    Therefore that’s why this piece of writing is amazing. Thanks!

  10. You have made some good points there. I looked on the internet for additional information about the issue and found most individuals will go
    along with your views on this web site.

  11. When someone writes an piece of writing he/she maintains the idea of
    a user in his/her brain that how a user can understand it.
    So that’s why this post is great. Thanks!

  12. I feel that is among the such a lot significant info for me.
    And i am happy studying your article. But should observation on some common issues, The website taste
    is great, the articles is truly excellent : D. Excellent activity,
    cheers

  13. In fact no matter if someone doesn’t understand then its up to
    other users that they will assist, so here it happens.

  14. www.fcc.gov表示:

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

  15. This is a really good tip especially to those new to the blogosphere.
    Brief but very precise information… Many thanks
    for sharing this one. A must read article!

  16. I all the time emailed this blog post page to all my contacts, since if like to
    read it then my links will too.

  17. It’s perfect time to make a few plans for the longer term and it is time to
    be happy. I’ve learn this submit and if I could
    I want to counsel you some fascinating things or tips.
    Perhaps you could write next articles relating to this article.

    I wish to read more things approximately it!

  18. „Teure Uhren” werden jene Uhren bezeichnet, welche über dem durchschnittlichen Preis von Armbanduhren Herren liegen. Meist werden auch die Armbänder mit diversen hochwertigen Materialien,
    wie z.B synthetische Edelsteine geschmückt.
    Derartige Uhren haben meist wertvolle Materialien im
    Gehäuse, auf der Anzeige oder im Uhrwerk eingearbeitet. Zudem sind
    in Einzelfällen auch zahlreiche Funktionen ein Ursprung für den Preis von Teure Uhren. Diese Zahl
    der Funktionen macht eine Uhr nämlich zu der chronographischen Uhr.
    Dies erhebt den Wert natürlich um den entsprechenden Wert.
    Bekannte Markenuhren welche vergleichsweise ziemlich teuer sind, sind i.

    a. Omega, Rolex, Breitling, Tag Heuer und Steinhart.
    Zudem sind auch generell Schweizer Uhren ziemlich qualitativ,
    was deren Preis himmelwärts gerichtet treibt. Aber auch Omega Seamaster Planet Ocean, Speedmaster, Carrera oder Aquaracer sind Marken hochwertiger, Teure Uhren. Zum Überfluss sind Teure Uhren ein sehr
    sicherer Geldspeicher. So können Sie sich beispielsweise eine einzigartige Teure Uhr für zirka 20.000€ zulegen und können sich von etwas wissen,
    dass sie in 20 Jahren genau den selben, oder gar einen höheren Wert hat.

  19. This page definitely has all of the information and facts I needed about this subject and didn’t know who to ask.

  20. Genuinely no matter if someone doesn’t understand then its
    up to other viewers that they will help, so here it happens.

  21. What’s up, its good post regarding media print, we all be aware of media is a impressive source
    of data.

  22. I do not even know how I ended up here, but I thought this
    post was great. I don’t know who you are but definitely you’re going to a famous blogger
    if you are not already 😉 Cheers!

  23. Hi! This is my first comment here so I just wanted to give a quick shout out and
    tell you I really enjoy reading your blog posts. Can you recommend any other blogs/websites/forums that deal with the
    same topics? Thanks a ton!

  24. What’s Taking place i am new to this, I stumbled upon this I’ve discovered It absolutely useful and it has aided me out loads.
    I hope to give a contribution & help different
    customers like its aided me. Good job.

  25. Hello! I know this is kinda off topic but I was wondering which
    blog platform are you using for this website?
    I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for
    another platform. I would be awesome if you could point me in the direction of a good platform.

  26. I go to see everyday a few web pages and sites to read articles, but this blog offers quality
    based writing.

  27. Hi to all, it’s genuinely a nice for me to
    go to see this site, it includes important Information.

  28. Hello there! This is my first visit to your blog! We are a group of volunteers
    and starting a new project in a community in the same niche.
    Your blog provided us useful information to work on. You have done a outstanding
    job!

  29. Hello there! Do you know if they make any plugins to help with Search
    Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing
    very good results. If you know of any please share.
    Many thanks!

  30. fantastic points altogether, you simply won a new reader. What would you recommend in regards to your post that you
    made a few days in the past? Any certain?

發佈留言

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