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,233,811 Responses

  1. Appreciate this post. Let me try it out.

  2. Thanks very interesting blog!

  3. I absolutely love your blog and find a lot of your post’s to be exactly I’m
    looking for. Would you offer guest writers to write content for you personally?
    I wouldn’t mind creating a post or elaborating on a number of the subjects you write about here.
    Again, awesome site!

  4. What i do not understood is in truth how you’re now not really a lot more neatly-liked than you
    might be now. You are very intelligent. You understand therefore
    considerably in the case of this subject, produced me in my opinion imagine it from a lot of
    numerous angles. Its like women and men aren’t interested until it
    is one thing to accomplish with Woman gaga!
    Your own stuffs excellent. At all times deal with it up!

  5. Hmm it appears like your blog ate my first comment (it was
    extremely 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 the whole thing. Do you have any points for inexperienced blog writers?
    I’d really appreciate it.

  6. I am genuinely happy to read this webpage posts which includes tons of valuable
    information, thanks for providing such information.

  7. brnk.in.ua表示:

    I like the valuable info you supply to your articles.
    I will bookmark your blog and check again right here frequently.
    I’m fairly certain I’ll learn plenty of new stuff right here!
    Best of luck for the next!

  8. Hi, its nice paragraph about media print, we all be familiar with media is a fantastic source of data.

  9. Saved as a favorite, I like your web site!

  10. Hurrah, that’s what I was exploring for, what a material!

    existing here at this webpage, thanks admin of this web site.

  11. discuss表示:

    Quality articles is the crucial to be a focus for the visitors to
    pay a quick visit the web site, that’s what this web
    page is providing.

  12. I am in fact grateful to the holder of this web site who has shared this
    enormous piece of writing at at this place.

  13. It’s an remarkable piece of writing designed for all the online
    people; they will obtain advantage from it I am sure.

  14. Marvelous, what a website it is! This website presents helpful
    information to us, keep it up.

  15. Great website you have here but I was curious if
    you knew of any discussion boards that cover the same topics
    discussed in this article? I’d really love to be a part of community where I can get advice from other experienced individuals that share the same interest.
    If you have any suggestions, please let me know. Thanks!

  16. Woah! I’m really digging the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between superb usability and visual appearance.
    I must say you have done a superb job with this. Also, the blog loads very fast for me
    on Internet explorer. Superb Blog!

  17. hey there and thank you for your info – I
    have definitely picked up something new from right here.

    I did however expertise a few technical points using this website, as I experienced to
    reload the website many times previous to I could get
    it to load properly. I had been wondering if your web hosting is OK?
    Not that I am complaining, but slow loading instances times will often affect your
    placement in google and could damage your high quality score if advertising and marketing with Adwords.
    Well I am adding this RSS to my email and can look out for a lot more
    of your respective intriguing content. Make sure you update this again soon.

  18. I am truly grateful to the owner of this web site who has shared this enormous article at at this place.

  19. Good web site you have here.. It’s hard to find high quality writing like yours
    these days. I honestly appreciate people like you!

    Take care!!

  20. After looking into a number of the blog posts on your blog,
    I seriously appreciate your way of blogging. I book-marked it to my
    bookmark webpage list and will be checking back in the near future.
    Take a look at my web site too and tell me your opinion.

  21. Yes! Finally someone writes about slot pulsa tanpa potongan.

  22. Hello, Neat post. There is a problem with your site in web explorer, might test this?
    IE nonetheless is the market chief and a big section of other folks will miss your great writing because of
    this problem.

  23. Thanks very nice blog!

  24. Appreciate this post. Let me try it out.

  25. What i don’t realize is in reality how you’re no longer really much more neatly-favored
    than you may be right now. You are very intelligent. You recognize
    thus significantly with regards to this topic, produced me personally believe it from so many numerous angles.
    Its like men and women don’t seem to be involved unless it is something to do with Lady gaga!
    Your individual stuffs great. All the time deal
    with it up!

  26. We’re a group of volunteers and starting a new scheme in our community.
    Your website provided us with useful information to work on.
    You’ve done an impressive job and our entire neighborhood can be grateful to you.

  27. I’ve been surfing online more than three hours as of late, but I
    never found any fascinating article like yours. It is lovely value enough for
    me. In my opinion, if all website owners and
    bloggers made just right content material as you did, the internet shall be a lot more helpful than ever before.

  28. First off I want to say terrific blog! I had a quick question in which I’d
    like to ask if you do not mind. I was curious to know how
    you center yourself and clear your thoughts before writing.
    I’ve had a tough time clearing my thoughts in getting my
    thoughts out there. I truly do enjoy writing however it just seems like the first 10 to 15 minutes tend
    to be wasted just trying to figure out how to begin. Any
    recommendations or tips? Appreciate it!

  29. Hi, this weekend is nice for me, for the reason that this moment
    i am reading this wonderful informative article here at
    my house.

  30. I am extremely impressed along with your writing skills and also with the
    structure on your weblog. Is this a paid topic or did you customize it your self?
    Either way stay up the nice quality writing, it is uncommon to peer a
    nice weblog like this one today..

發佈留言

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