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,232,840 Responses

  1. Theda表示:

    What’s Going down i’m new to this, I stumbled upon this I have found It positively useful and
    it has aided me out loads. I’m hoping to contribute & help
    different users like its helped me. Great job.

  2. Excellent beat ! I wish to apprentice while you amend your site,
    how could i subscribe for a blog website? The account aided me a acceptable deal.
    I had been tiny bit acquainted of this your broadcast offered bright clear concept

  3. Spot on with this write-up, I honestly feel this web site needs much
    more attention. I’ll probably be back again to see more, thanks for the advice!

  4. This paragraph is really a good one it assists new web visitors, who are wishing in favor of
    blogging.

  5. blip.fm表示:

    Hi everyone, it’s my first pay a visit at this web site, and paragraph is in fact fruitful in support of me, keep up posting these articles.

  6. It’s great that you are getting ideas from this paragraph as
    well as from our discussion made here.

  7. Marc表示:

    A person necessarily help to make severely articles I would state.
    This is the very first time I frequented your
    web page and to this point? I amazed with the research you made to make this actual publish incredible.
    Great job!

  8. I am truly happy to glance at this weblog posts which contains plenty of valuable information, thanks for
    providing these kinds of data.

  9. jbr mlm hk表示:

    I am regular visitor, how are you everybody? This article posted at
    this web page is genuinely good.

  10. anotepad.com表示:

    Admiring the persistence you put into your site and detailed information you
    offer. It’s good to come across a blog every once in a
    while that isn’t the same outdated rehashed material. Excellent read!
    I’ve saved your site and I’m adding your
    RSS feeds to my Google account.

  11. Can you tell us more about this? I’d like to find out more
    details.

  12. Hey, I think your blog might be having browser compatibility issues.
    When I look at your blog site in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, superb blog!

  13. Amazing! This blog looks just like my old one! It’s on a totally different topic but it has pretty much the same layout and design. Wonderful choice of colors!

  14. Great site you’ve got here.. It’s hard to find good quality
    writing like yours these days. I seriously appreciate individuals
    like you! Take care!!

  15. What’s up to all, because I am actually eager of reading this web site’s post to be updated on a regular basis.

    It includes good data.

  16. Elliott表示:

    I am extremely impressed with your writing skills as well as with the layout on your
    blog. Is this a paid theme or did you modify it yourself? Either way
    keep up the excellent quality writing, it is rare to see a great blog like this one nowadays.

  17. Great blog! Do you have any tips for aspiring writers?
    I’m planning to start my own blog soon but I’m a little lost on everything.

    Would you propose starting with a free platform like
    Wordpress or go for a paid option? There are so many options out there that I’m
    totally confused .. Any ideas? Thank you!

  18. Yesterday, while I was at work, my sister 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 broken and
    she has 83 views. I know this is entirely off
    topic but I had to share it with someone!

  19. I am curious to find out what blog platform you are using?
    I’m experiencing some small security problems with my latest website and I’d like to find something more secure.
    Do you have any recommendations?

  20. Katherine表示:

    Hi! Do you know if they make any plugins to help with
    Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Appreciate it!

  21. Excellent way of explaining, and nice piece of writing to obtain information about my presentation subject matter,
    which i am going to deliver in school.

  22. Hey there! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly? My site looks weird when viewing from my iphone4.
    I’m trying to find a template or plugin that might be able to fix this problem.
    If you have any recommendations, please share. Cheers!

  23. discuss表示:

    Great post. I used to be checking constantly
    this blog and I’m inspired! Extremely useful information specifically the final section 🙂 I maintain such information much.

    I was seeking this particular info for a
    long time. Thank you and best of luck.

  24. What’s up everyone, it’s my first go to see at this
    site, and piece of writing is in fact fruitful designed for me, keep up posting these content.

  25. www.fcc.gov表示:

    Good way of telling, and pleasant post to obtain facts about my presentation subject, which i am going to deliver in college.

  26. Hello! I realize this is sort of off-topic but I needed to ask.
    Does running a well-established blog like yours require a massive amount work?
    I’m completely new to blogging but I do write in my journal everyday.
    I’d like to start a blog so I can easily share my personal experience and feelings
    online. Please let me know if you have any kind of ideas or tips for brand
    new aspiring blog owners. Thankyou!

  27. Carmen表示:

    Great article! We will be linking to this particularly great
    post on our site. Keep up the good writing.

  28. discuss表示:

    Great post. I was checking continuously this blog and I
    am impressed! Very helpful information particularly the last
    part 🙂 I care for such information much. I was seeking this certain info for a very long
    time. Thank you and best of luck.

  29. That is really fascinating, You are a very professional blogger.
    I have joined your rss feed and look ahead to in search of more of your great post.
    Also, I have shared your web site in my social networks

發佈留言

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