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

838,671 Responses

  1. I used to be suggested this website by means of my cousin. I’m
    now not positive whether this submit is written through him as nobody else understand such detailed approximately my difficulty.
    You’re amazing! Thanks!

  2. Quality articles or reviews is the important to be a focus for the visitors to pay a visit the web site,
    that’s what this web page is providing.

  3. Terrific article! This is the kind of information that are meant to be shared around
    the net. Disgrace on the seek engines for
    not positioning this publish upper! Come on over and visit my website .
    Thank you =)

  4. Pretty! This has been a really wonderful post. Many thanks for supplying these details.

  5. Hello to all, how is the whole thing, I think every one is getting more from this website, and your views are
    good designed for new people.

  6. Great post but I was wanting to know if you could
    write a litte more on this subject? I’d be very thankful if you could elaborate a
    little bit more. Thanks!

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

  8. Thanks for finally writing about > JS 依照元素出現數量排序
    – 馬老師 雲端研究室 < Liked it!

  9. Wow that was strange. I just wrote an really long comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb blog!

  10. Thank you for the auspicious writeup. It in fact was a
    amusement account it. Look advanced to more added agreeable
    from you! By the way, how can we communicate?

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

  12. I constantly spent my half an hour to read this
    website’s posts everyday along with a cup of coffee.

  13. Great work! That is the type of info that are meant to be shared
    around the net. Disgrace on Google for no longer positioning this submit higher!
    Come on over and visit my website . Thank
    you =)

  14. Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I
    acquire actually enjoyed account your blog posts. Any way I will be subscribing to your augment and even I achievement you access consistently rapidly.

  15. My brother suggested I might like this website.
    He used to be entirely right. This submit truly made my day.
    You cann’t consider simply how much time I had spent for
    this information! Thank you!

  16. Oh my goodness! Awesome article dude! Thank you so much, However I am going through difficulties with your RSS.
    I don’t understand the reason why I am unable to join it.
    Is there anybody else having the same RSS problems?
    Anybody who knows the answer can you kindly respond? Thanks!!

  17. Howdy, There’s no doubt that your website may be having browser compatibility problems.
    Whenever I look at your website in Safari, it looks fine but
    when opening in IE, it has some overlapping issues.

    I just wanted to give you a quick heads up!
    Aside from that, great site!

  18. That is very interesting, You’re an overly professional blogger.
    I’ve joined your feed and look ahead to searching for extra of your excellent post.
    Also, I have shared your website in my social networks

  19. It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about!
    Thanks

  20. It’s actually very complicated in this full of activity life to listen news on Television,
    therefore I simply use internet for that reason, and take the latest news.

  21. This information is priceless. Where can I find out more?

  22. What’s up to all, how is the whole thing, I think every one is getting more from this web page, and your
    views are fastidious in favor of new users.

  23. Ashely表示:

    Hi there, just became aware of your blog through Google,
    and found that it is really informative. I’m gonna watch out for brussels.
    I’ll be grateful if you continue this in future. Numerous
    people will be benefited from your writing. Cheers!

  24. Hi there, I desire to subscribe for this blog to get most up-to-date updates, so
    where can i do it please assist.

  25. What’s up colleagues, good post and fastidious urging commented at this place, I am
    truly enjoying by these.

  26. Do you mind if I quote a couple of your posts as long as
    I provide credit and sources back to your blog?

    My blog is in the very same area of interest as yours and my users would genuinely benefit from some of the information you
    provide here. Please let me know if this ok with
    you. Thanks!

  27. Hmm is anyone else experiencing problems with the pictures on this blog loading?
    I’m trying to find out if its a problem on my end or if it’s the blog.
    Any responses would be greatly appreciated.

  28. Yes! Finally something about situs judi slot online.

  29. I do believe all the concepts you’ve offered to your post.
    They’re really convincing and can certainly work.

    Still, the posts are too quick for novices.
    Could you please lengthen them a little from subsequent time?

    Thank you for the post.

發佈留言

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