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,232,136 Responses

  1. RandallTaulp表示:

    lisinopril 30 mg daily: lisinopril brand name cost – can i order lisinopril over the counter

  2. Ten Things Your Competitors Learn About Motorcycle Accident Litigation Motorcycle Accident Attorneys

  3. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# ciprofloxacin

  4. Ronaldpsymn表示:

    http://finasteride.store/# cost generic propecia no prescription

  5. Lurlene表示:

    The Sage Advice On Ranking SEO Software From An Older Five-Year-Old suchmaschinenoptimierung software (Lurlene)

  6. Aimee表示:

    What’s The Reason Mesothelioma Law Is Fast Becoming The Hottest Fashion Of 2023 asbestos law (Aimee)

  7. 10 Meetups On Double Glazing Repairs Near Me You Should Attend Double glazing Repairs

  8. How The 10 Worst Titration ADHD Medications Fails Of All Time Could Have Been Avoided Adhd Titration Process

  9. Ronaldpsymn表示:

    https://cytotec.club/# buy cytotec pills

  10. 20 Up-Andcomers To Watch The Mental Health Psychiatrist Near Me Industry Mental Health Assessments Online

  11. Davidtes表示:

    buy cipro ciprofloxacin order online buy ciprofloxacin over the counter

  12. RandallTaulp表示:

    buy cipro online canada: buy cipro – ciprofloxacin

  13. cheap表示:

    Find Out More About Windows And Doors Leeds When You Work
    From At Home cheap

  14. Brittany表示:

    The No. 1 Question Everyone Working In CSGO New Case Must Know How
    To Answer hydra case (Brittany)

  15. RandallTaulp表示:

    buy tamoxifen: nolvadex estrogen blocker – what happens when you stop taking tamoxifen

  16. buy gsa ser表示:

    20 Things You Should Have To Ask About GSA SER Ranker Before You Buy GSA SER Ranker buy gsa ser

  17. Five Killer Quora Answers To Renault Clio Replacement Key Card renault Clio replacement key card

  18. vimeo.Com表示:

    The Next Big New Shopping Online Industry lossless Audio cable – vimeo.Com

  19. WilliamRhype表示:

    https://finasteride.store/# generic propecia price

  20. 15 Gifts For The CSGO Most Profitable Cases Lover In Your Life Operation wildfire Case

  21. RandallTaulp表示:

    buy cytotec over the counter: order cytotec online – buy cytotec online

  22. Williamked表示:

    Организация свадьбы под ключ https://yagodawedding.ru/ с гарантией. Быстрый расчет сметы, работаем с любым бюджетом. Берём на себя абсолютно все свадебные хлопоты. Организуем одни из самых красивых свадебных и семейных событий.

  23. Why Combination Washer Dryer In One Isn’t As Easy As You
    Think Washer dryers cheap

  24. Davidtes表示:

    Misoprostol 200 mg buy online buy misoprostol over the counter buy cytotec in usa

  25. Ronaldpsymn表示:

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

  26. Sarong表示:

    You could certainly see your expertise within the work you write.

    The sector hopes for more passionate writers like you who are not afraid to say how they believe.
    At all times go after your heart.

  27. double表示:

    Guide To Double Glazing Windows Repairs: The Intermediate Guide To
    Double Glazing Windows Repairs double

  28. Davidtes表示:

    buy cytotec purchase cytotec buy cytotec pills online cheap

  29. Davidtes表示:

    generic propecia tablets propecia tablet order generic propecia pills

  30. I do not know whether it’s just me or if everyone else encountering issues with your site.
    It appears as if some of the text within your content are running off the screen. Can somebody else please comment and let me know if this is happening to them too?
    This could be a issue with my web browser because I’ve had this happen before.
    Appreciate it

發佈留言

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