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

837,342 Responses

  1. Gilberto表示:

    hi!,I like your writing very a lot! percentage we keep in touch
    more about your article on AOL? I need a specialist in this space to solve my
    problem. May be that is you! Having a look forward to see you.

  2. important review. On top of that visit this kind of
    site to try out agen slot pulsa. Fantastic web page has received recommendation via slot
    online gaming cricket fanatics in Indonesia. Truth be told
    there tend to be many appealing promos in case you join these days.

  3. I have been surfing online more than 4 hours today, yet I never found any interesting article
    like yours. It is pretty worth enough for me. In my view, if all webmasters and bloggers made good content as you did, the internet will be a
    lot more useful than ever before.

  4. آموزش آنلاین زبان ، مهارت برای زندگی | همه
    چی زبان
    آموزش‌های کاربردی از مهم ترین کتاب های بازار
    درباره همه چی زبان
    در این وبسایت به دانش عظیم و کشف دنیا با یادگرفتن زبان جدید می
    رسید

  5. Alfonzo表示:

    My family members all the time say that I am killing my time here at web, but I know I am getting knowledge every day by reading thes pleasant articles or reviews.

  6. BUY IPTV表示:

    What’s up friends, pleasant post and nice urging commented
    here, I am actually enjoying by these.

  7. These are actually wonderful ideas in on the topic of blogging.
    You have touched some nice factors here. Any way keep up wrinting.

  8. Fantastic beat ! I wish to apprentice while you amend
    your site, how could i subscribe for a blog web site?
    The account helped me a acceptable deal. I had been a little bit
    acquainted of this your broadcast provided bright clear
    concept

  9. Woah! I’m really enjoying the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s hard to get
    that “perfect balance” between user friendliness
    and visual appeal. I must say that you’ve done a fantastic job with this.
    In addition, the blog loads extremely fast for me on Chrome.
    Outstanding Blog!

  10. Hiya! Quick question that’s completely off topic. Do you
    know how to make your site mobile friendly? My website
    looks weird when viewing from my iphone. I’m trying to find a theme or plugin that
    might be able to correct this problem. If you
    have any suggestions, please share. Thank you!

  11. ask.fm表示:

    Hello would you mind stating which blog platform you’re
    working with? I’m going to start my own blog soon but I’m having a tough time making
    a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and I’m
    looking for something unique. P.S Apologies for being off-topic but
    I had to ask!

  12. You actually make it seem so easy with your
    presentation but I find this topic to be actually something which I think I
    would never understand. It seems too complex and extremely broad for me.
    I’m looking forward for your next post, I’ll
    try to get the hang of it!

  13. This design is incredible! You obviously 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 loved what you had to say, and more than that, how you presented it.
    Too cool!

  14. sony pictures表示:

    Hi, I do think this is a great web site. I stumbledupon it 😉
    I will revisit once again since i have saved as a favorite it.
    Money and freedom is the best way to change, may you be rich and
    continue to guide others.

  15. Bridgette表示:

    WOW just what I was looking for. Came here by searching for website

  16. Hello, every time i used to check website posts here early
    in the break of day, since i love to find out more and more.

  17. Wow, amazing blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your web site is wonderful,
    let alone the content!

  18. Great blog here! Also your site loads up fast! What web host
    are you using? Can I get your affiliate link to your host?

    I wish my site loaded up as fast as yours lol

  19. If some one desires expert view concerning blogging and site-building after that i recommend him/her to visit this weblog,
    Keep up the nice work.

  20. judi online表示:

    I was able to find good advice from your blog articles.

  21. Hi there! I simply want to give you a huge thumbs up
    for the excellent info you have here on this post.

    I’ll be coming back to your website for more soon.

  22. chinese porn表示:

    Every weekend i used to pay a visit this website, because
    i wish for enjoyment, as this this site conations genuinely good
    funny data too.

  23. There is certainly a great deal to learn about this issue.
    I like all of the points you made.

  24. www.ted.com表示:

    hi!,I really like your writing very a lot! proportion we be in contact extra about
    your post on AOL? I need an expert in this space to resolve my problem.

    Maybe that’s you! Taking a look forward to see you.

  25. Pretty nice post. I just stumbled upon your
    blog and wished to mention that I have really loved surfing
    around your weblog posts. After all I will be
    subscribing for your feed and I’m hoping you write again soon!

  26. peatix.com表示:

    Hey There. I found your blog the usage of msn. That is a very smartly written article.

    I’ll be sure to bookmark it and return to learn more of
    your useful info. Thanks for the post. I will definitely comeback.

  27. I’m really loving the theme/design of your site.
    Do you ever run into any web browser compatibility
    problems? A few of my blog audience have complained about my blog not operating correctly in Explorer but
    looks great in Safari. Do you have any tips to help fix this issue?

  28. 230news.com表示:

    Keep on writing, great job!

  29. Very nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog
    posts. In any case I’ll be subscribing to your rss feed and I hope you write again soon!

  30. I’m really enjoying the theme/design of your blog.
    Do you ever run into any internet browser compatibility issues?

    A couple of my blog audience have complained about my blog not
    operating correctly in Explorer but looks great in Safari.
    Do you have any ideas to help fix this issue?

發佈留言

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