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,628,790 Responses

  1. Right here is the perfect webpage for anyone who would like
    to understand this topic. You realize so much its almost hard to argue with you (not that I actually would want to…HaHa).

    You definitely put a brand new spin on a topic which has been written about for
    many years. Excellent stuff, just excellent!

  2. For hottest information you have to visit internet and on the web I found this web site as a finest web site for newest updates.

  3. I was wondering if you ever thought of changing the structure of your
    site? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way
    of content so people could connect with it better. Youve got an awful lot of text for only
    having one or two pictures. Maybe you could
    space it out better?

  4. Adele表示:

    This text is priceless. How can I find out more?

  5. Wonderful views on that!

  6. daftar slot表示:

    Thanks in support of sharing such a good opinion, post is pleasant, thats why i have read it
    completely

  7. www.fcc.gov表示:

    I’d like to thank you for the efforts you’ve put in penning this site.
    I really hope to view the same high-grade
    blog posts by you in the future as well. In truth, your creative writing abilities has motivated me
    to get my own, personal site now 😉

  8. After going over a handful of the articles on your web page, I truly like your technique of writing a blog.
    I added 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 how you feel.

  9. 230-V-Netz dienen lediglich der Veranschaulichung des jeweiligen Schaltschemas.
    Bitte bedenken Sie, dass Arbeiten und Installationen an der Stromversorgung ausschließlich
    dafür ausgebildeten Fachkräften vorgenommen werden sollten.
    Bei Aktiv sein der Elektroinstallation ist zudem immer sicherzustellen, dass
    die Sicherung ausgeschaltet oder herausgedreht wurde.

    Außerdem können in Bestandsgebäuden nicht den Vorschriften entsprechende Elektroinstallationen (Farbe der Leitungen, Verlegung der Leitungen usw.) sein. Zudem muss die Belegung der Leitungen eindeutig definiert
    werden können. Zunächst an der Zeit sein, die Anschlussmöglichkeiten an einem Bewegungsmelder richtig zu interpretieren. Auch am späteren Verbraucher sind diese drei Anschlussmöglichkeiten gegeben: L, N
    und PE. Ansonsten können auch Lichtschalter in die
    Schaltung des Bewegungsmelders integriert werden. Wie Sie einen Bewegungsmelder zum Ausschalten schalten,
    zeigen wir in dem verlinkten Beitrag ausführlich.
    Auch die Schaltung des Bewegungsmelders bzw.
    des Verbrauchers auf Dauerstrom zeigen wir hier ausführlich.
    In diesem Zusammenhang wird L vorm Bewegungsmelder ebenfalls durch einen Wippschalter geführt, dann aber zusätzlich direkt mit L des Verbrauchers verklemmt.
    Kurz umrissen: Vor dem Bewegungsmelder wird L zusätzlich durch
    einen Wippschalter geführt und dann weiter zu L bzw. L2 am Bewegungsmelder.
    Eine weitere Möglichkeit ist es, eine Schaltung für mehrere Bewegungsmelder anzulegen. Natürlich können Sie die Schaltung per se vornehmen, dass Sie den Bewegungsmelder komplett ausschalten und
    den Verbraucher dennoch auf Dauerstrom schalten können. Dazu können Sie die Bewegungsmelder
    in Reihe schalten. Beachten Sie bei dieser Schaltung jedoch, dass sich die Schaltzeiten beider Bewegungsmelder addieren können, wenn Bewegungen innerhalb
    beider Überwachungsbereiche erfolgen.

  10. v.gd表示:

    It’s an remarkable post for all the internet viewers; they will get
    benefit from it I am sure.

  11. hi!,I like your writing very so much! share we be in contact extra approximately your article on AOL?
    I need an expert in this space to unravel my problem.
    May be that is you! Having a look ahead to see you.

  12. Wonderful work! This is the kind of information that are meant to be shared
    across the web. Shame on Google for not positioning this
    post higher! Come on over and visit my site .
    Thank you =)

  13. Hello! I simply want to give you a huge thumbs up for your great information you have here on this post.
    I will be returning to your blog for more soon.

  14. judi online表示:

    Hello to every one, for the reason that I am truly
    eager of reading this weblog’s post to be updated on a regular
    basis. It carries pleasant data.

  15. result表示:

    Hi, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments?
    If so how do you prevent it, any plugin or anything you can suggest?
    I get so much lately it’s driving me crazy so any help is very much appreciated.

  16. madu sumbawa表示:

    If some one desires to be updated with most recent technologies therefore he must be pay a quick visit
    this web site and be up to date all the time.

  17. I know this if off topic but I’m looking into starting my own blog and was curious what all is
    required to get set up? I’m assuming having a blog
    like yours would cost a pretty penny? I’m not very web savvy so I’m not 100%
    sure. Any suggestions or advice would be greatly appreciated.
    Thanks

  18. Hello there, just became alert to your blog through
    Google, and found that it’s really informative. I am gonna watch out for brussels.
    I’ll appreciate if you continue this in future.
    A lot of people will be benefited from your writing. Cheers!

  19. www.fcc.gov表示:

    Amazing issues here. I am very glad to peer your post.
    Thanks a lot and I am looking forward to touch
    you. Will you please drop me a e-mail?

  20. Hey would you mind stating which blog platform you’re
    working with? I’m looking to start my own blog in the near
    future but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique.

    P.S My apologies for getting off-topic but I had to ask!

  21. I love it when people come together and share thoughts.
    Great blog, keep it up!

  22. Thank you for any other wonderful article.
    The place else could anybody get that kind of info in such an ideal
    manner of writing? I’ve a presentation next week, and I am on the look
    for such info.

  23. Howdy! I know this is kind of 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 issues with hackers and I’m looking at alternatives
    for another platform. I would be awesome if you could point me in the direction of
    a good platform.

  24. 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 clearly know what youre talking about, why throw away your intelligence on just posting videos to your blog when you could be giving
    us something informative to read?

  25. Laurie表示:

    Stunning story there. What occurred after? Thanks!

  26. Unquestionably believe that which you stated.

    Your favorite reason appeared to be on the internet the simplest thing to be aware of.
    I say to you, I definitely get annoyed while people consider worries that they just
    don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having
    side-effects , people can take a signal. Will likely be back to get
    more. Thanks

  27. nordicwalking表示:

    Nordic walking ist eine Sportart, die bei Erwachsenen in allen Altersgruppen sehr beliebt ist.
    Nordicwalking ist einfach zu erlernen. Sie eignet sich
    auch für Menschen, die nach längerer Zeit in ein Training einsteigen möchten und oder
    nach einer Erkrankung ihre Fitness wiedererlangen möchten. Sie benötigen keine speziellen Voraussetzungen oder
    Spezialkleidung und können das Zubehör einfach und schnell
    bei Sportco bestellen. Das wichtigste Sportgerät fürt Sportart sind die Nordic-Walking-Stöcke.
    Gut beraten sein wählen Sie ein stufenlos einstellbares Modell.
    Für den guten Halt sorgen Nordic-Walking-Handschuhe.

    Diese sind besonders für lange Touren wichtig.
    Achten Sie auf die richtige Nordic-Walking-Stocklänge; diese
    ist für die gesunde Ausübung der Sportart sehr wichtig.
    Wenn Sie Handschuhe tragen, können Sie die Nordic-Walking-Stöcke besser halten. Für verschiedene Untergründe benötigen Sie spezielle Sportart Pads, die Sie an den Nordic-Walking-Stöcken einfach wechseln können. Schrittzähler helfen Ihnen, den Überblick über
    Ihre Leistung zu behalten. Für die Sicherheit schleierhaft sorgen Reflektoren und Blinklichter.

    Das richtige Pad gibt Ihnen ausreichend Grip für die Ausübung des Sports.
    Ein Elastikband zur Anbringung an einem Nordic-Walking-Stock
    ermöglicht ein zusätzliches Kraft-Widerstands-Training e.
    g. für die Arme.

  28. vimeo.com表示:

    Hello there! I know this is kinda off topic
    however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa?

    My site goes over a lot of the same subjects as yours and I
    think we could greatly benefit from each other. If you might
    be interested feel free to shoot me an email.
    I look forward to hearing from you! Great blog by the way!

  29. Hey! Would you mind if I share your blog with my zynga group?
    There’s a lot of people that I think would really enjoy your content.

    Please let me know. Many thanks

發佈回覆給「http://www.giant.org.cn/space-uid-105632.html」的留言 取消回覆

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