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,231,686 Responses

  1. Guide To Upvc Repairs Near Me: The Intermediate Guide Towards
    Upvc Repairs Near Me upvc repairs near Me

  2. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy ciprofloxacin

  3. The 10 Most Scariest Things About Electric Fireplace Wall Mounted electric fireplace Wall

  4. 7 Simple Secrets To Totally Intoxicating Your Private ADHD
    Assessment Cost Uk Adhd Assessment Private

  5. 10 Factors To Know About Cerebral Palsy Litigation You Didn’t
    Learn In The Classroom cerebral Palsy lawsuit

  6. RandallTaulp表示:

    ciprofloxacin 500mg buy online: ciprofloxacin generic – buy cipro online canada

  7. Rosaura表示:

    Are You In Search Of Inspiration? Check Out Online Shop Designer
    Suits Oriental Furniture Fishbowl (Rosaura)

  8. WilliamRhype表示:

    https://lisinopril.network/# lisinopril online without a prescription

  9. RandallTaulp表示:

    buy lisinopril no prescription: lisinopril average cost – lisinopril 25

  10. Greetings! I’ve been reading your blog for some time now and finally got the bravery to go ahead and give you a shout out from Porter Tx!

    Just wanted to tell you keep up the good work!

  11. 10 Misconceptions That Your Boss May Have Concerning Online
    Store Uk Cheapest 30G Desiccant Packs (https://vimeo.com)

  12. The Reason Why Malpractice Claim Has Become Everyone’s Obsession In 2023
    Malpractice Lawsuits

  13. Window Repair表示:

    Five Killer Quora Answers To Window Repairman Window Repair

  14. Window Repair表示:

    15 Fun And Wacky Hobbies That’ll Make You Better At Double Glazing Windows Repair Window Repair

  15. WilliamRhype表示:

    http://lisinopril.network/# lisinopril 2.5

  16. mensvault.men表示:

    Why We Why We Best CS GO Case Opening Sites
    (And You Should Too!) case glove (mensvault.men)

  17. 10 Things Everyone Hates About Autolocksmiths Nearby Auto Locksmith

  18. Looking for a bitcoin casino wizard casino with plenty of games,
    you should check out Casinoroyale.bet. The casino offers secure and fair gambling opportunities.

  19. RandallTaulp表示:

    lisinopril metoprolol: lisinopril 40 mg without prescription – lisinopril online without prescription

  20. Davidtes表示:

    cytotec abortion pill buy cytotec online buy cytotec over the counter

  21. Dylan表示:

    10 Misconceptions Your Boss Holds About Shopping Online Sites
    List Luxury Bathroom Faucet – Dylan

  22. Davidtes表示:

    lisinopril 2 how to buy lisinopril prinivil lisinopril

  23. How To Choose The Right Defra Exempt Stove On The Internet Smoke control area stoves

  24. Aurum表示:

    I just like the helpful info you provide to your articles.
    I’ll bookmark your blog and check again here regularly.
    I’m relatively sure I will be told many new stuff proper here!
    Good luck for the following!

    my web blog – Aurum

  25. RandallTaulp表示:

    lisinopril 20 mg price online: prinivil 10 mg – prinivil 5mg tablet

  26. A Sage Piece Of Advice On Double Glazing In Leeds
    From The Age Of Five Patio Door Repair Leeds

  27. RandallTaulp表示:

    cytotec buy online usa: buy cytotec pills online cheap – buy cytotec pills

  28. WilliamRhype表示:

    https://finasteride.store/# propecia brand name

  29. RandallTaulp表示:

    cipro 500mg best prices: buy ciprofloxacin – cipro generic

發佈留言

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