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,156,338 Responses

  1. What To Focus On When Making Improvements To Motor Vehicle Compensation Motor vehicle Accident

  2. How To Tell The Good And Bad About Search Engine Optimisation Search Engine Optimisation Software (Tujuan.Grogol.Us)

  3. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you! However,
    how could we communicate?

  4. You’ll Never Guess This Private ADHD Titration’s Benefits private adhd titration

  5. memek enak表示:

    Good post however , I was wanting to know if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate a little bit further.
    Thanks!

  6. система для конференц зала https://www.oborudovanie-konferenc-zalov11.ru .

  7. What’s The Job Market For Mesothelioma Lawyer Professionals?
    Mesothelioma lawyer

  8. Kelvin表示:

    If some one wishes expert view about running a blog afterward i propose him/her
    to go to see this blog, Keep up the nice job.

  9. 15 Documentaries That Are Best About Birth Defect Case Birth Defect Law Firms

  10. Hi, I think your blog might be having browser compatibility issues.
    When I look at your website in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that, superb blog!

  11. What’s up it’s me, I am also visiting this site daily, this website is really good and the visitors are in fact
    sharing pleasant thoughts.

  12. Richard表示:

    lesbian porn

  13. law表示:

    8 Tips To Increase Your Motor Vehicle Claim Game law

  14. Jonah表示:

    10 Inspirational Graphics About Upvc Door Handles upvc double doors
    (Jonah)

  15. BomesJoype表示:

    Lorem Ipsum has been the industry’s standard dummy text https://abc-datenservice.de/qrreimj of type and scrambled it to make a type specimen book.

  16. Guide To Double Glazed Window Near Me: The Intermediate Guide In Double
    Glazed Window Near Me Double Glazed Window Near Me

  17. The most effective location for installation is in an open room that gets good air flow.

  18. See What Window Repairs Near Me Tricks The Celebs Are Utilizing window Repairs near me

  19. With crypto providing decentralized betting, there are far more opportunities to bet anonymously as the technology improves.

  20. Just wish to say your article is as astonishing. The clearness in your
    post is simply cool and i can assume you’re 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.

  21. The Worst Advice We’ve Heard About Mattresses Double Size Mattress Double Sale

  22. TAMPUNG99表示:

    That is a really good tip especially to those new to the blogosphere.

    Simple but very accurate information… Many thanks for sharing this one.
    A must read article!

  23. 5 Lessons You Can Learn From Workers Compensation Lawyers
    workers’ Compensation Attorney

發佈留言

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