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,249,900 Responses

  1. discuss表示:

    I’m more than happy to uncover this web site. I wanted to thank you for your time due to
    this fantastic read!! I definitely liked every little
    bit of it and i also have you book marked to look at new stuff on your site.

  2. fensterguru表示:

    Die Preise in der Tabelle sind natürlich nur beispielhafte Preise für einzelne Fenstermodelle unterschiedlicher
    Hersteller. Sie geben aber bereits einen guten Eindruck darüber, wie
    unterschiedlich hoch die Preise für vergleichbar große Fenster oft
    sein können. Kostencheck-Experte: Hier müssen zahlreiche
    Faktoren berücksichtigt werden, die hier mit einfließen. Frage:
    Wovon hängt der Preis für ein dreifach verglastes Fenster grosso modo ab?
    Lesen Sie auch – Fenster renovieren – welche Kosten muss man rechnen? Lesen Sie auch – Fenster-Einbau durch
    den Fachmann: Welche Kosten an der Zeit sein rechnen? Das haben ja auch unsere Preisbeispiele bereits gezeigt.
    Die Ausführung eines Fensters nämlich kann also sehr voneinander
    abweichen, dadurch können selbst bei vergleichbar großen Fenstern riesige Preisunterschiede entstehen.
    Frage: Inwieweit spielen die Rahmenmaterialien eine Rolle für den Preis?

    Das zeigt beispielhaft auch die nachstehende Tabelle.

    Kostencheck-Experte: Das Material, aus dem der Rahmen grundlegend besteht, hat eine ganz wesentliche preisentscheidende Wirkung.
    Zwischen Kunststoff und Aluminium liegen in unserem Beispiel beinahe 100 % Preisunterschied.
    Das zeigt, welchen Einfluss allein das Rahmenmaterial auf den Preis der Fenster hat.
    Hochgerechnet auf die Fenster eines ganzen Hauses kann das die Kosten also beinahe verdoppeln.

  3. I?m not that much of a online reader to be honest but your
    blogs really nice, keep it up! I’ll go ahead and bookmark your website
    to come back in the future. All the best

  4. www.ted.com表示:

    I’m really impressed with your writing skills and also with the layout on your weblog.
    Is this a paid theme or did you modify it yourself?
    Either way keep up the nice quality writing, it’s
    rare to see a nice blog like this one today.

  5. Marvelous, what a web site it is! This webpage provides valuable
    information to us, keep it up.

  6. Highly descriptive blog, I liked that a lot. Will there be a part 2?

  7. The other day, while I was at work, my cousin stole my
    iPad and tested to see if it can survive a 30 foot drop, just
    so she can be a youtube sensation. My iPad is now broken and
    she has 83 views. I know this is totally off topic
    but I had to share it with someone!

  8. 80.82.64.206表示:

    If you desire to grow your familiarity only keep visiting this
    web page and be updated with the newest information posted here.

  9. botdb.win表示:

    Hi, just wanted to say, I liked this blog post.
    It was helpful. Keep on posting!

  10. Aw, this was an incredibly good post. Spending some time and actual effort to generate a great article… but what can I say… I hesitate a whole lot and don’t manage to
    get anything done.

  11. PianoMan表示:

    These kind of post are always inspiring and I prefer to read quality content so I happy to stumble on many decent point here in the post, writing is simply huge, thank you for
    the post

  12. madu sumbawa表示:

    Thanks for some other informative blog. Where
    else may I get that type of information written in such a perfect means?
    I’ve a undertaking that I’m simply now running on, and I have been at the
    glance out for such info.

  13. Good day! I could have sworn I’ve been to this site before but after
    reading through some of the post I realized it’s new to me.
    Anyways, I’m definitely glad I found it and I’ll be book-marking and checking back frequently!

  14. Helpful info. Lucky me I found your website unintentionally, and
    I am surprised why this twist of fate didn’t took place in advance!
    I bookmarked it.

  15. Just desire to say your article is as amazing.
    The clarity to your post is simply great and i could think you are knowledgeable in this subject.
    Well together with your permission let me to snatch your feed to stay up to date
    with drawing close post. Thank you a million and please continue the rewarding work.

  16. I’m pretty pleased to find this website. I want to to thank you for your time for this
    wonderful read!! I definitely loved every little bit of it
    and I have you bookmarked to look at new information in your website.

  17. I simply could not depart your website before suggesting that I really enjoyed the usual information an individual supply on your visitors?
    Is gonna be again regularly to check out new posts.

  18. First off I want to say wonderful blog! I had a quick
    question in which I’d like to ask if you don’t mind.
    I was curious to know how you center yourself and clear your head before writing.
    I have had trouble clearing my mind in getting my
    ideas out there. I do take pleasure in writing but it just seems
    like the first 10 to 15 minutes are generally lost just trying to figure out how to begin. Any recommendations or
    tips? Thanks!

  19. Awsome blog! I am loving it!! Will be back later to read some more.
    I am taking your feeds also

  20. Titan CBD oil表示:

    Hello, I believe your site might be having web browser compatibility problems.
    Whenever I look at your web site in Safari, it looks fine
    however, if opening in IE, it’s got some overlapping issues.
    I merely wanted to provide you with a quick
    heads up! Aside from that, wonderful website!

  21. I’m curious to find out what blog platform you’re working
    with? I’m experiencing some minor security problems with my latest site and I’d like to find
    something more safeguarded. Do you have any suggestions?

  22. Hi there, You have done an incredible job. I’ll certainly digg it and personally suggest
    to my friends. I am sure they’ll be benefited from this website.

  23. My partner and I stumbled over here different website and thought I might
    check things out. I like what I see so now i’m following you.
    Look forward to finding out about your web page yet again.

  24. kassenrollen表示:

    Die Steuerberatergenossenschaft DATEV empfiehlt dazu,
    man allen grund haben darüber vor beim zuständigen Finanzamt erkundigen. Damit stellt man nicht
    nur das Finanzamt zufrieden, sondern sichert auch seine Ansprüche
    auf Gewährleistung. So oder so gilt für Handwerkerrechnungen eine Aufbewahrungsfrist von zwei Jahren sowie von fünf Jahren für Belege
    von Bauleistungen am Eigenheim. Wenn der Absender etwa über sein Fakturaprogramm eine elektronische PDF-Rechnung qualifiziert signiert, errechnet
    die Software über das gesamte Dokument einen Hashwert und
    kodiert diesen dabei privaten Schlüssel des Absenders.
    Dieser Schlüssel ist beispielsweise auf einem USB-Stick gespeichert und speziell für
    den Absender zugänglich, wenn er beim Signieren im Rechner eingesteckt ist.
    Das Zertifikat enthält Verweise auf seinen Herausgeber, den vom Herausgeber beglaubigten Namen und den öffentlichen Schlüssel des
    Absenders. Stimmen beide Werte überein, kann man auf die Unversehrtheit der
    Rechnung vertrauen. Außerdem kann der Adobe Reader durch Online-Anfrage beim Herausgeber eine Auskunft über
    Echtheit und Gültigkeit des Zertifikats einholen, das sogenannte OCSP (Online Certificate Status Protocol).

    Öffnet der Empfänger die erhaltene Rechnung etwa im Adobe Reader,
    errechnet dieser den Hashwert des Dokuments und dekodiert übern öffentlichen Schlüssel des Absenders den mitgelieferten Hash.
    Dieses Protokoll ist auch bei wiederkehrenden Rechnungen desselben Gläubigers
    von Mal zu Mal wie gehabt einzuholen.Article_Title Bonrollen & Thermorollen & EC-Rollen

  25. www.fcc.gov表示:

    Right away I am ready to do my breakfast, once having my breakfast coming
    yet again to read further news.

  26. Thanks for sharing your thoughts on bacgiang.tcvn.gov.vn.
    Regards

  27. I think the admin of this site is truly working hard in support of his web site,
    as here every material is quality based information.

  28. Cool information it is surely. I’ve been seeking for this update.

  29. pbase.com表示:

    Hey! This is my first comment here so I just wanted to give a quick shout
    out and tell you I truly enjoy reading your articles.
    Can you recommend any other blogs/websites/forums that cover the same subjects?
    Thank you!

  30. menwiki.men表示:

    Hello this is somewhat of off topic but I was wanting to know
    if blogs use WYSIWYG editors or if you have to manually
    code with HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get
    advice from someone with experience. Any help would be enormously appreciated!

發佈留言

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