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,228,071 Responses

  1. You’ll Never Guess This Adult Adhd Assessment’s Benefits Adhd Assessment

  2. Ronaldpsymn表示:

    https://finasteride.store/# cheap propecia tablets

  3. Will Double Glazing Repair Near Me Ever Be The King Of
    The World? wooden Windows double glazed

  4. Davidtes表示:

    nolvadex 20mg tamoxifen hormone therapy tamoxifen hot flashes

  5. 서재응表示:

    I admire your piece of work, thankyou for all the informative content 서재응.

  6. Davidtes表示:

    buy cytotec Abortion pills online buy cytotec

  7. fitters表示:

    Seven Reasons To Explain Why Window Repair Near Is
    Important fitters

  8. Fantastic beat ! I wish to apprentice while you amend your site, how
    could i subscribe for a blog site? The account helped me a acceptable deal.
    I had been a little bit acquainted of this your broadcast offered bright clear idea

  9. RandallTaulp表示:

    buy generic propecia without a prescription: cost cheap propecia price – cost of propecia

  10. WilliamRhype表示:

    https://cytotec.club/# purchase cytotec

  11. Guide To Upvc Repairs Near Me: The Intermediate Guide In Upvc Repairs Near Me
    upvc repairs Near me

  12. film porno表示:

    I’ve been exploring for a little for any
    high-quality articles or blog posts on this kind of house .
    Exploring in Yahoo I finally stumbled upon this
    website. Reading this info So i’m satisfied to express that I’ve a very excellent uncanny feeling
    I found out just what I needed. I such a lot unquestionably will make sure to do not forget this web site and provides it
    a look on a relentless basis.

  13. What’s The Job Market For Titration ADHD Adults Professionals?
    Titration adhd

  14. Who Is Responsible For An Mesothelioma Attorney Budget?
    12 Tips On How To Spend Your Money asbestos compensation

  15. Davidtes表示:

    buy cytotec online buy misoprostol over the counter order cytotec online

  16. See What L30 Ultra Dreame Tricks The Celebs Are Using l30 Ultra dreame

  17. WilliamRhype表示:

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

  18. Cheri表示:

    What’s The Current Job Market For Window Repairs Leeds Professionals Like?
    window repairs leeds (Cheri)

  19. Responsible For A Ghost Immobiliser Installation Budget?
    10 Unfortunate Ways To Spend Your Money ghost Immobiliser installation

  20. RandallTaulp表示:

    lisinopril 50 mg price: lisinopril price – zestoretic 20 25

  21. near By表示:

    11 Ways To Completely Redesign Your Car Keys Replacement
    Near Me near By

  22. pvc表示:

    The 3 Most Significant Disasters In Double Glazing Supplies Near Me The Double
    Glazing Supplies Near Me’s 3 Biggest Disasters In History
    pvc

  23. Ronaldpsymn表示:

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

  24. Nine Things That Your Parent Taught You About
    Glass Replacement Windows Glass Replacement Windows

  25. WilliamRhype表示:

    http://nolvadex.life/# where can i buy nolvadex

  26. Ronaldpsymn表示:

    https://cytotec.club/# Abortion pills online

  27. Davidtes表示:

    п»їbuy lisinopril 10 mg uk lisinopril 20 mg pill buy lisinopril without prescription

發佈留言

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