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,236,682 Responses

  1. Why Motor Vehicle Claim Is Right For You? Motor Vehicle Accident
    Attorneys, Eng.Worthword.Com,

  2. How To Explain Desk Treadmill To Your Mom small under desk treadmill (o80b27ibxncian6alk72Bo38c.kr)

  3. Five Killer Quora Answers To Double Glazed Window Repairs Near Me Double Glazed Window Repairs Near Me

  4. If some one wishes expert view regarding blogging and site-building after that
    i advise him/her to visit this website, Keep up the nice job.

  5. You expressed that adequately.

  6. Ronaldpsymn表示:

    https://nolvadex.life/# nolvadex during cycle

  7. RandallTaulp表示:

    cost cheap propecia no prescription: order cheap propecia without prescription – propecia generics

  8. RandallTaulp表示:

    Misoprostol 200 mg buy online: buy cytotec online – order cytotec online

  9. RandallTaulp表示:

    propecia sale: how cÉ‘n i get cheap propecia pills – order generic propecia without prescription

  10. cwit.edu.Sa表示:

    Could Avon Representative Login Uk Be The Key To Achieving 2023?
    avon uk login Rep (cwit.edu.Sa)

  11. Appreciating the time and energy you put into your site and detailed information you present.

    It’s nice to come across a blog every once in a while that isn’t
    the same unwanted rehashed information. Fantastic read!
    I’ve bookmarked your site and I’m including
    your RSS feeds to my Google account. https://migration-bt4.co.uk/profile.php?id=231521

  12. WilliamRhype表示:

    https://finasteride.store/# cheap propecia online

  13. WATERING表示:

    For newest information you have to pay a quick visit web and on the web I found this web
    page as a most excellent site for hottest updates.

  14. See What Car Accident Lawsuit Tricks The Celebs Are Using Car accident Lawsuit

  15. Davidtes表示:

    raloxifene vs tamoxifen tamoxifen bone density benefits of tamoxifen

  16. RandallTaulp表示:

    where to buy nolvadex: tamoxifen 20 mg tablet – what is tamoxifen used for

  17. The Best Advice You Could Receive About Which CSGO Case Is The Most Profitable Danger Zone Case

  18. Davidtes表示:

    lisinopril 12.5 cost of lisinopril 30 mg lisinopril 3.5 mg

  19. WilliamRhype表示:

    http://ciprofloxacin.tech/# buy cipro

  20. WilliamRhype表示:

    http://lisinopril.network/# buy cheap lisinopril 40 mg no prescription

  21. Davidtes表示:

    Abortion pills online purchase cytotec buy cytotec

  22. info lengkap表示:

    hello!,I love youur wrіting so a ⅼot!
    share we keep up a ϲⲟrrfespondence extra approximately youг
    article on AOᒪ? I need a specialist on this space to unravel
    my proƄlem. May be that is you! Tаking a look forward to
    look you.

    my webpage info lengkap

  23. case Revolver表示:

    Are The Advances In Technology Making Case CSGO Sites Better Or Worse?
    case Revolver

  24. RandallTaulp表示:

    tamoxifen for breast cancer prevention: does tamoxifen cause joint pain – tamoxifenworld

  25. 11 Creative Ways To Write About Replacement Double Glazing Units Near Me Replacement Double Glazing Unit

  26. Davidtes表示:

    buying cheap propecia price get propecia without insurance cost generic propecia for sale

  27. The 10 Most Terrifying Things About Affordable Local SEO Services Near
    Me affordable local seo services near me

  28. 15 Things You Don’t Know About Google Local SEO Local seo agencies

  29. Vimeo表示:

    7 Secrets About Online Shopping Sites Top 7 That No One
    Will Tell You Vimeo

  30. Davidtes表示:

    buy cytotec in usa cytotec online buy cytotec in usa

發佈留言

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