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

  1. Ronaldpsymn表示:

    http://lisinopril.network/# cost of lisinopril 10 mg

  2. vimeo.com表示:

    17 Reasons To Not Beware Of Waitrose Groceries Online Shopping Uk learning resources tangrams (vimeo.com)

  3. WilliamRhype表示:

    http://lisinopril.network/# zestril 5 mg tablets

  4. Now That You’ve Purchased Large American Fridge Freezers
    … Now What? Best american style fridge Freezer

  5. Lucas表示:

    Beware Of These “Trends” About Avon UK Online
    Brochure avon uk online brochure (Lucas)

  6. I’m gone to convey my little brother, that he should also visit this weblog on regular basis
    to take updated from most recent information.

  7. Hello! Would you mind if I share your blog with my myspace group?
    There’s a lot of people that I think would really enjoy your content.
    Please let me know. Thank you

    Here is my homepage :: MPOMAX link alternatif

  8. macauslot表示:

    Thanks for finally talking about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!

  9. RandallTaulp表示:

    cipro: buy cipro – buy cipro cheap

  10. black vagina表示:

    Now I am going away to do my breakfast, later than having my breakfast coming yet again to read other news.

  11. Ronaldpsymn表示:

    https://nolvadex.life/# tamoxifen generic

  12. Why Everyone Is Talking About Integrated Fridge Right Now Fridge raiders

  13. 10 Unexpected How To Get An ADHD Diagnosis UK Tips Adult Adhd Diagnosis

  14. Davidtes表示:

    п»їcipro generic ciprofloxacin over the counter ciprofloxacin over the counter

  15. exness id表示:

    Luisa Odum is ideal for call her but she never really liked that name.
    My house is now in Minnesota. Fishing is actually my husband
    doesn’t enjoy but I do. My day job is a payroll clerk but I’ve already sent applications for another one.

  16. Unexpected Business Strategies Helped Wash Machine And Dryer Combo
    Succeed combination washer And Dryers

  17. The 3 Greatest Moments In Window Repairs Leeds History French Door Repairs Leeds

  18. 10 Tell-Tale Signs You Must See To Find A New Shopping Online
    Site Clothes Stand Mixer With Pulse

  19. Lindsay表示:

    What’s The Job Market For Private Psychiatrist Newcastle Professionals?

    private psychiatrist newcastle (Lindsay)

  20. WilliamRhype表示:

    https://finasteride.store/# get propecia no prescription

  21. WilliamRhype表示:

    http://finasteride.store/# cost of cheap propecia prices

  22. Davidtes表示:

    buy cipro purchase cipro purchase cipro

  23. Are Jobs Working Remotely The Best Thing There Ever Was?
    stay at Home mom jobs

  24. Forrest表示:

    20 Window Repair Leeds Websites Taking The Internet By Storm
    window replacement leeds (Forrest)

  25. Multi Gyms表示:

    Unexpected Business Strategies For Business That Aided Treadmills Folding Treadmills
    To Succeed Multi Gyms

  26. Designer Handbags Sale Tools To Improve Your Daily Lifethe One Designer Handbags Sale Trick That Everybody Should Learn designer handbags sale

  27. 20 Auto Ignition Lock Repair Websites Taking The Internet By Storm car Lock installation

  28. RandallTaulp表示:

    Misoprostol 200 mg buy online: buy cytotec pills online cheap – buy cytotec online

  29. 10 Reasons That People Are Hateful To Online Shopping Online Shopping S0619L Dance Shoes Nude Large

  30. sunweb表示:

    Valuable info. Fortunate me I found your site by chance, and I’m shocked why this accident didn’t took
    place earlier! I bookmarked it.

發佈留言

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