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,374,961 Responses

  1. RamonRek表示:

    ozempic: ozempic cost – ozempic generic

  2. Hurrah! After all I got a website from where I can in fact get
    helpful facts regarding my study and knowledge.

    Feel free to visit my web-site: day care center neglect

  3. Mei Ramirez表示:

    5 Killer Quora Answers To Land Rover Key Replacement Cost Uk Land rover key replacement cost uk

  4. Te.Legra.Ph表示:

    The History Of Auto Locksmith Near Me Prices Locksmith Auto Keys (Te.Legra.Ph)

  5. USP human growth hormone (somatropin).

    Here is my web page … functional mushroom gummies

  6. Jeremypag表示:

    buy ozempic buy cheap ozempic buy cheap ozempic

  7. Bookmarks表示:

    I was pretty pleased to uncover this web site.
    I need to to thank you for your time for this wonderful
    read!! I definitely savored every little bit of it and I
    have you bookmarked to see new information on your site.

    Here is my blog Bookmarks

  8. I read this paragraph completely concerning the difference of hottest and earlier technologies, it’s awesome
    article.

    Feel free to surf to my web-site: day care institutions

  9. Bookmarks表示:

    It’s time to look into energy comparison.

    Check out my web page Bookmarks

  10. bookmarks表示:

    NewULife Ranks. Invite to my NewUlife Review.

    my homepage … bookmarks

  11. Bookmarks表示:

    I was extremely pleased to discover this site. I want to to thank you for your time due
    to this wonderful read!! I definitely appreciated every bit of it and
    i also have you book marked to see new information in your site.

    my web blog :: Bookmarks

  12. Bookmarks表示:

    The vapor and flavor top quality is likewise excellent.

    Here is my blog … Bookmarks

  13. Hi there! I know this is kinda off topic but I was wondering if
    you knew where I could find a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having difficulty finding one?
    Thanks a lot!

  14. Hi, just wanted to mention, I loved this article.

    It was practical. Keep on posting!

    my page … daycare center accounting

  15. Bookmarks表示:

    Hello would you mind letting me know which web host you’re using?
    I’ve loaded your blog in 3 completely different web browsers and I must say this blog loads a lot faster then most.
    Can you suggest a good web hosting provider at a honest price?
    Thanks, I appreciate it!

    Feel free to surf to my website :: Bookmarks

  16. In this instance, the life insurance policy business’s money.

    Review my web page :: euphoria mushroom gummies

  17. What is New U Life SOMADERM Transdermal Gel.

    Feel free to visit my site – social cbd muscle balm stick lavender

  18. All gas meters offer the same tariffs for usage.

    my website :: compare business electricity

  19. Yes, solar energy definition chemistry panels will continue to work in the winter.

  20. XYGENYX, a licensing business for FDA-registered products.

    Feel free to surf to my homepage – Euphoria Mushroom Gummies

  21. Wow! This could be one particular of the most useful blogs
    We’ve ever arrive across on this subject. Actually
    Fantastic. I’m also a specialist in this topic therefore I can understand your hard
    work.

    My web site :: child care craft

  22. 평택호빠表示:

    평택호빠폭넓게 모집중이므로, 자신감 갖고 도전하셨으면 좋겠습니다.

  23. Large centers表示:

    Thank you for helping out, wonderful info.

    Also visit my website … Large centers

  24. Bookmarks表示:

    Most heat from the sun arrives as infrared energy.

    Feel free to surf to my web-site Bookmarks

  25. KONTOL HITAM表示:

    Thanks for a marvelous posting! I quite enjoyed reading
    it, you happen to be a great author.I will always bookmark your blog and will often come back later on. I want to encourage one to continue your great writing,
    have a nice day!

  26. Bookmarks表示:

    Hiya, I am really glad I’ve found this information. Today bloggers publish
    only about gossips and internet and this is actually frustrating.

    A good site with exciting content, that is what I need.
    Thank you for keeping this web site, I’ll be visiting it.

    Do you do newsletters? Cant find it.

    Here is my blog post … Bookmarks

  27. 10 Meetups About Situs 4d You Should Attend toto Macau

  28. BarryDof表示:

    https://ozempic.art/# buy ozempic pills online

  29. Frankevild表示:

    https://ozempic.art/# ozempic generic

  30. I love it when individuals come together and share thoughts.
    Great blog, continue the good work!

發佈留言

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