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,174 Responses

  1. Heya i am for the primary time here. I found this board and I to find It
    truly useful & it helped me out much. I’m hoping to provide something again and aid others such
    as you helped me.

  2. This design is steller! You most certainly know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great
    job. I really enjoyed what you had to say, and more than that,
    how you presented it. Too cool!

  3. www.fcc.gov表示:

    Hi there, always i used to check weblog posts here early in the daylight,
    for the reason that i love to find out more and more.

  4. bonrollen表示:

    Um eine Gefährdung der Gesundheit bei einem Einsatz von Thermopapieren inwendig von Lebensmitteln auszuschließen, sind diese Papiere gesetzlichen Auflagen unterworfen. Das Institut ISEGA
    in Aschaffenburg ist eine Forschungsgesellschaft, die zahlreiche Werkstoffe neben Verschiedenem auf den Gehalt von Bishpenol A hin untersucht.
    Thermopapiere, die das ISEGA oder das so genannte Swan Zertifikat tragen, werden als unbedenklich eingestuft.
    Thermodrucker erzeugen ihr Druckbild per einer Walze, die das Papier an einer erhitzten Leiste vorbeiführt.

    Die Leiste ist reich an kleinen Heizelementen ausgestattet, die über die komplette Breite des Druckkopfes
    angebracht sind. Die vielen kleinen Heizelemente werden Druckpixel genannt.
    Die Farbstoffe auf dem Thermopapier aufnehmen so genannten unterschiedlichen Leuco Farbstoffen. Die Hitze verursacht auf der temperaturempfindlichen Schicht des
    Thermopapiers eine Reaktion, durch die Farbe ausgebildet wird.
    Diese natürlichen Farbstoffe zeigen sich
    im nicht angeregtem Zustand als weiße Farbe.
    Die Farbstoffe reagieren zusammen hiermit Entwickler erst unter Hitzeeinwirkung.
    Durch Zufuhr von Hitze werden die Farbstoffe als Farbe sichtbar.
    Das Papier trägt eine Schicht aus Farbstoffen, die aufm weißen Untergrund des Materials unsichtbar sind.
    Unter Zusatz von weiteren Farbstoffen erzeugen die Leuco
    Farbstoffe farbige oder schwarze Druckbilder.

  5. What a data of un-ambiguity and preserveness of precious know-how on the topic of
    unexpected emotions.

  6. Hi, i think that i saw you visited my web site so i
    came to “return the favor”.I am trying to find things to improve my
    site!I suppose its ok to use some of your ideas!!

  7. I was wondering if you ever considered changing the structure of your blog?
    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 images.
    Maybe you could space it out better?

  8. Hmm it seems like your site ate my first comment (it was super long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog writer but I’m still new to everything.
    Do you have any helpful hints for inexperienced blog writers?
    I’d definitely appreciate it.

  9. Very nice post. I just stumbled upon your blog and wished to say that I have truly enjoyed browsing your blog posts.
    After all I will be subscribing to your rss feed and I
    hope you write again very soon!

  10. I absolutely love your blog.. Excellent colors & theme. Did you build this website yourself?

    Please reply back as I’m planning to create my own blog and would love to find out where
    you got this from or what the theme is called. Many thanks!

  11. Hello There. I found your blog using msn. This is a very well written article.

    I’ll be sure to bookmark it and return to read more of your useful information. Thanks
    for the post. I’ll certainly return.

  12. I don’t know whether it’s just me or if perhaps
    everybody else experiencing problems with your blog.
    It appears as though some of the text within your content are running
    off the screen. Can someone else please provide feedback and let me
    know if this is happening to them too? This might be a
    issue with my browser because I’ve had this happen previously.
    Kudos

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

  14. What’s up, this weekend is good designed for me, for the
    reason that this moment i am reading this impressive informative
    article here at my house.

  15. really interesting review. Also visit this specific site in an effort to game slot.

    That awesome web page has received identity through situs slot online gambling men and
    women in Negara sendiri. Generally there will be
    loads of attractive promos if you ever join today.

  16. At this time it appears like Expression Engine is
    the top blogging platform available right
    now. (from what I’ve read) Is that what you are using on your
    blog?

  17. discuss表示:

    Hey there! Someone in my Myspace group shared this website with
    us so I came to look it over. I’m definitely enjoying
    the information. I’m bookmarking and will be tweeting this to my followers!

    Exceptional blog and terrific style and design.

  18. Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a 40 foot drop,
    just so she can be a youtube sensation. My apple ipad is now
    destroyed and she has 83 views. I know this is completely off topic
    but I had to share it with someone!

  19. Great article! We are linking to this great article on our site.
    Keep up the good writing.

  20. discuss表示:

    I used to be able to find good advice from your blog posts.

  21. Marilyn表示:

    Hello, of course this post is in fact nice and I have learned lot of
    things from it concerning blogging. thanks.

  22. Hello, this weekend is fastidious in favor of
    me, since this time i am reading this impressive informative post here at my house.

  23. list.ly表示:

    Great delivery. Outstanding arguments. Keep up the amazing effort.

  24. Hi there, just wanted to say, I loved this article.
    It was inspiring. Keep on posting!

  25. Jayson表示:

    Quality articles or reviews is the main to interest the users to
    visit the website, that’s what this website is providing.

  26. Appreciate this post. Will try it out.

  27. A motivating discussion is worth comment. I believe that you ought
    to write more about this subject, it may not be a taboo matter
    but usually folks don’t discuss these topics. To the next!
    Best wishes!!

  28. wonderful points altogether, you just gained a logo new reader.
    What could you suggest about your submit that you made some days in the past?
    Any sure?

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

  30. Thanks , I have just been looking for information about this subject for a while
    and yours is the greatest I have discovered till now. But, what about the bottom line?

    Are you sure about the source?

發佈留言

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