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,230,675 Responses

  1. WilliamRhype表示:

    http://cytotec.club/# buy cytotec online

  2. Ugzhnkchr.Ru表示:

    Nine Things That Your Parent Teach You About Mini Cooper Key Fob Replacement
    Mini Cooper Key Fob Replacement [Ugzhnkchr.Ru]

  3. seosoftware表示:

    What Is SEO Software And Why Is Everyone Speakin’ About
    It? seosoftware

  4. Davidtes表示:

    Abortion pills online cytotec buy online usa cytotec abortion pill

  5. Ronaldpsymn表示:

    http://cytotec.club/# buy cytotec over the counter

  6. Davidtes表示:

    cytotec pills buy online purchase cytotec buy cytotec over the counter

  7. Elissa表示:

    Prada Bag Crossbody Tools To Improve Your Daily Life Prada Bag
    Crossbody Trick That Every Person Should Know prada bag crossbody (Elissa)

  8. vimeo表示:

    15 Of The Top Best Online Shopping Groceries Uk Bloggers You Should Follow vimeo

  9. You’ll Be Unable To Guess Double Glaze Repair Near Me’s Benefits double glaze repair near me

  10. RandallTaulp表示:

    ciprofloxacin mail online: ciprofloxacin over the counter – ciprofloxacin 500 mg tablet price

  11. 14 Savvy Ways To Spend Extra Erb’s Palsy Compensation Budget erb’s palsy lawsuit

  12. Marisol表示:

    The Leading Reasons Why People Perform Well At The Secondary Double
    Glazing Near Me Industry secondary double glazing near
    me (Marisol)

  13. RandallTaulp表示:

    get generic propecia pills: get propecia without a prescription – propecia prices

  14. WilliamRhype表示:

    http://nolvadex.life/# nolvadex online

  15. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy cipro online canada

  16. Ronaldpsymn表示:

    http://finasteride.store/# get cheap propecia without insurance

  17. What Is It That Makes Double Glazed Units Near Me So Famous?

    Replacement Double Glazed Units Near Me

  18. The Steve Jobs Of Leeds Windows And Doors Meet One Of The Leeds Windows And Doors Industry’s Steve
    Jobs Of The Leeds Windows And Doors Industry New Glazing leeds

  19. RandallTaulp表示:

    cost propecia without rx: cheap propecia online – generic propecia

  20. Sven表示:

    The No. 1 Question Anyone Working In Mesothelioma Compensation Should Be Able To Answer asbestos settlement (Sven)

  21. These are actually great ideas in on the topic of blogging.
    You have touched some pleasant things here. Any way keep
    up wrinting.

  22. Why Green Mobility Is The Best Choice For You? green mobility scooter;
    cameradb.review,

  23. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# ciprofloxacin over the counter

  24. WilliamRhype表示:

    https://nolvadex.life/# natural alternatives to tamoxifen

  25. Karl表示:

    Guide To Designer Handbags For Women: The Intermediate Guide In Designer Handbags For Women designer handbags for women (Karl)

  26. WilliamRhype表示:

    http://finasteride.store/# buy propecia online

  27. The Reason Why Peugeot 307 Key Replacement Will Be Everyone’s Desire In 2023 peugeot Car Key replacement

發佈留言

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