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

863,774 Responses

  1. s.id表示:

    I enjoy reading a post that can make men and women think.
    Also, thanks for allowing for me to comment!

  2. What’s up, its fastidious paragraph concerning media print, we all be familiar with media is a great source of information.

  3. Great article. I am experiencing many of these issues as
    well..

  4. What’s up, just wanted to tell you, I liked this post. It was practical.
    Keep on posting!

  5. rolex watches表示:

    A fascinating discussion is definitely worth
    comment. There’s no doubt that that you ought to write more on this subject matter, it might not be
    a taboo matter but usually folks don’t talk about these subjects.
    To the next! All the best!!

  6. First off I would like to say great blog! I had a quick question which
    I’d like to ask if you do not mind. I was curious to know how you center yourself and clear your thoughts
    prior to writing. I’ve had a difficult time clearing my mind
    in getting my ideas out there. I do enjoy writing but it just seems like the first
    10 to 15 minutes tend to be lost simply just trying to figure out how
    to begin. Any ideas or hints? Cheers!

  7. 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ür das Sport sind die Nordic-Walking-Stöcke. Es empfiehlt
    sich 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 geboren 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 ein rätsel
    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
    neben Verschiedenem für die Arme.

  8. Thanks for sharing your thoughts. I really appreciate your efforts and I
    am waiting for your further post thanks once again.

  9. discuss表示:

    Howdy I am so happy I found your weblog, I really found you by error,
    while I was searching on Yahoo for something
    else, Nonetheless I am here now and would just like to say
    thanks for a remarkable post and a all round entertaining blog (I also love the theme/design), I don’t have time to read it all at
    the minute but I have bookmarked it and also added your RSS feeds,
    so when I have time I will be back to read a great deal more, Please do keep up the awesome
    work.

  10. discuss表示:

    Excellent post. I was checking constantly this blog and I’m impressed!

    Extremely helpful information particularly the last part 🙂 I care for such info a lot.
    I was looking for this certain information for a very
    long time. Thank you and best of luck.

  11. aescat表示:

    Hi I am so grateful I found your site, I really found you by accident, while I was browsing on Digg for something else,
    Anyways I am here now and would just like to say thanks a lot for a incredible post and a all round exciting blog (I
    also love the theme/design), I don’t have time to
    browse it all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read
    much more, Please do keep up the superb job.

  12. It’s awesome designed for me to have a web page, which is beneficial designed for my knowledge.
    thanks admin

  13. Hi, i believe that i saw you visited my site so i got here to go back the favor?.I am attempting to to
    find issues to improve my website!I assume its ok to
    use a few of your concepts!!

  14. Wow, marvelous blog layout! How long have you been blogging for?

    you make blogging look easy. The overall look
    of your web site is great, let alone the content!

  15. This article will assist the internet visitors for building up new
    blog or even a blog from start to end.

  16. Your method of explaining the whole thing in this paragraph is in fact fastidious, every
    one be capable of easily understand it, Thanks a lot.

  17. Hi there! This post could not be written any better!
    Reading through this post reminds me of my good old room mate!
    He always kept talking about this. I will forward this article to
    him. Fairly certain he will have a good read.

    Thank you for sharing!

  18. Hello, Neat post. There’s a problem along with your web site in web
    explorer, could test this? IE nonetheless is the market chief and a good section of other folks will omit
    your excellent writing due to this problem.

  19. I must thank you for the efforts you’ve put in penning this site.
    I really hope to check out the same high-grade content from you in the future
    as well. In truth, your creative writing abilities has motivated me to get
    my very own site now 😉

  20. discuss表示:

    It’s very simple to find out any matter on web as compared to books, as I found this piece of writing at this site.

  21. Awesome things here. I’m very glad to look your post.
    Thank you so much and I’m taking a look ahead to touch you.
    Will you please drop me a mail?

  22. Terrific post however , I was wondering if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate
    a little bit more. Many thanks!

  23. It’s very simple to find out any matter on web as compared
    to textbooks, as I found this article at this website.

  24. When someone writes an piece of writing he/she retains the thought of a user in his/her brain that
    how a user can be aware of it. So that’s why this paragraph is
    perfect. Thanks!

  25. Yes! Finally someone writes about cutt.ly.

  26. Thank you, I have just been searching for information approximately
    this topic for ages and yours is the best I have found out so far.

    But, what in regards to the conclusion? Are you positive about the
    source?

  27. I am actually delighted to read this weblog posts which carries
    tons of useful data, thanks for providing these information.

  28. It’s really a cool and helpful piece of information.
    I’m glad that you just shared this helpful info with
    us. Please keep us up to date like this. Thank you for sharing.

  29. Pretty! This has been an incredibly wonderful post.
    Thank you for providing this information.

發佈留言

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