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

  1. What’s up to every body, it’s my first visit of this blog;
    this blog includes amazing and truly excellent information designed for visitors.

    Here is my web page: 商業登記證費用

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

  3. Excellent way of telling, and nice post to take data concerning my presentation topic, which i am
    going to deliver in institution of higher education.

  4. I have to thank you for the efforts you have put in writing this site.
    I’m hoping to check out the same high-grade content by you later on as well.
    In truth, your creative writing abilities has encouraged me to get my very
    own site now 😉

  5. KevinSounc表示:

    https://slotsiteleri.bid/# slot bahis siteleri

  6. KevinSounc表示:

    https://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  7. 7 Things About Suzuki Car Keys You’ll Kick Yourself For Not Knowing suzuki alto remote key

  8. Hi there friends, pleasant article and good arguments commented here, I am actually enjoying
    by these.

  9. Simply desire to say your article is as astounding. The clearness in your post is just great and i
    could assume you are an expert on this subject.

    Well with your permission allow me to grab your feed to keep updated
    with forthcoming post. Thanks a million and please continue the rewarding work.

  10. porn表示:

    incest porn

  11. Seat Replacement Key Cost Tips That Will Change Your Life car Remote replacement

  12. Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again. Regardless, just wanted to say fantastic blog!

    Have a look at my web page: lottery defeater software price

  13. sandibet表示:

    Aw, this was an incredibly nice post. Taking the time and actual effort to generate a very good article… but what can I say… I hesitate a
    whole lot and never manage to get nearly anything done.

  14. Hi there to all, how is everything, I think every one
    is getting more from this website, and your views are pleasant designed for new visitors.

  15. I am curious to find out what blog platform you happen to
    be using? I’m experiencing some minor security issues with my latest website and
    I’d like to find something more risk-free. Do you have any solutions?

  16. What i don’t realize is actually how you’re not really much more neatly-liked than you may be now.
    You’re so intelligent. You realize thus significantly when it
    comes to this topic, produced me for my part consider it from numerous varied angles.
    Its like men and women are not involved unless it is something to do with Lady gaga!

    Your own stuffs excellent. All the time handle it up!

  17. Hello! I’m at work browsing your blog from my new iphone
    4! Just wanted to say I love reading through your
    blog and look forward to all your posts! Keep up the outstanding work!

  18. Slot Uang表示:

    Have you ever thought about publishing an e-book or guest authoring on other blogs?
    I have a blog based upon on the same topics you discuss and would love to have you share some stories/information.
    I know my visitors would enjoy your work. If you are even remotely interested, feel free to shoot me an e-mail.

  19. Darrelherse表示:

    slot kumar siteleri: slot bahis siteleri – bonus veren casino slot siteleri

  20. Good blog post. I certainly love this site. Keep writing!

  21. Ev Taşıma | Ev taşıma sürecinde doğru planlamanın ne kadar önemli olduğunu bu yazınızda çok güzel anlatmışsınız. Kozcuoğlu Ev Taşıma olarak, her detayı titizlikle planlayarak müşterilerimize kusursuz bir taşınma deneyimi sunuyoruz.

  22. game online表示:

    This paragraph provides clear idea in support of the new visitors
    of blogging, that truly how to do running a blog.

  23. I visited many web pages however the audio feature for audio songs present at this
    web page is really excellent.

  24. Darrelherse表示:

    yasal slot siteleri: en cok kazandiran slot siteleri – slot oyunlar? siteleri

  25. Hello there, I discovered your site by way of Google while looking for
    a comparable subject, your site got here up, it seems good.
    I’ve bookmarked it in my google bookmarks.
    Hi there, just became alert to your blog via Google, and located that
    it’s really informative. I’m going to be careful for brussels.
    I’ll appreciate in the event you continue this in future. Many people shall be benefited from your writing.

    Cheers!

  26. Darrelherse表示:

    slot bahis siteleri: slot kumar siteleri – slot siteleri bonus veren

發佈留言

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