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,227,936 Responses

  1. Davidtes表示:

    zestril medicine lisinopril 60 mg lisinopril 5 mg tablet price in india

  2. WilliamRhype表示:

    http://finasteride.store/# cost generic propecia tablets

  3. WilliamRhype表示:

    http://lisinopril.network/# buy lisinopril online canada

  4. Davidtes表示:

    nolvadex estrogen blocker tamoxifen rash pictures п»їdcis tamoxifen

  5. ak47表示:

    I think this is among the most significant information for me.

    And i’m glad reading your article. But wanna remark
    on some general things, The web site style is great, the articles is really excellent : D.

    Good job, cheers

  6. Coin Auctions表示:

    Hello! Would you mind if I share your blog with my
    twitter group? There’s a lot of folks that I think would really appreciate your content.

    Please let me know. Thank you

    Look at my web-site :: Coin Auctions

  7. Kudos. I appreciate this!

  8. free fire max diamond top up
    free fire india top up
    codashop free fire top up
    free fire diamond top up
    free fire top up
    garena free fire top up
    free fire diamond top up
    top up free fire max
    top up free fire
    top-up garena free fire
    free fire diamonds top up
    garena ff diamond top up
    top up free fire max
    garena free fire max top up
    garena free fire max top-up
    top up ff max
    free fire diamond shop
    free fire max top up
    garena free fire top up diamonds
    free fire top up offer
    codashop free fire top up
    free fire diamond buy
    ff diamond top up
    ff diamonds top up
    garena free fire max top-up
    top up free fire
    free fire diamonds buy
    ff diamond top up
    free fire max top up
    free fire max diamond top up
    free fire id top up
    free fire account top up
    free fire max top up center
    top up ff max
    ff max top-up
    ff top up
    garena topup free fire
    free fire diamonds top up
    free fire diamond top up
    free fire top-up
    free fire top up website in india
    free fire top up codashop
    garena top up center ff
    free fire top up diamonds
    free fire top up diamond
    free fire top up india codashop
    garena diamond top up free fire
    top up diamond free fire codashop
    ff max top-up
    ff max top-up diamond
    ff max top-up diamonds
    double diamond top up codashop
    free fire top up centre
    free fire top up center
    codashop free fire max paytm
    diamonds free fire top up
    diamond free fire top up
    free fire special airdrop top up online
    top up free fire diamond
    top up free fire diamonds
    codashop com free fire
    garena free fire max top up center
    garena free fire top-up
    garena free fire topup
    free fire diamond recharge
    diamond top-up free fire
    diamonds top-up free fire
    free fire diamond top up app
    free fire diamond top up
    free fire diamond top up site
    garena free fire diamond top up
    free fire diamond top up website
    monthly membership in free fire
    free fire garena top up
    codashop ff top up
    free fire top-up website
    top up in free fire
    garena free fire max diamond top up
    garena free fire max diamonds top up
    free fire diamonds top up app
    top up free fire diamonds
    top up free fire diamond
    free fire double diamond top up codashop
    garena free fire top up center
    free fire diamond purchase
    free fire top up india
    winzo free fire diamond top up
    winzo free fire diamonds top up
    free fire diamonds purchase
    free fire diamond purchase
    diamond top up in free fire
    free fire top up diamond
    codashop free fire diamond india
    free fire top up app
    free fire top up garena
    free fire topup
    shop garena sg diamond
    shop garena sg diamonds
    freefire topup discount
    top up free fire max
    top up garena free fire max
    freefire topup
    garena free fire top up
    shop garena free fire
    diamond top up free fire
    top up garena free fire
    freefire top up
    top up game free fire
    top up game free fire max
    free fire online top up
    free fire paytm top up
    free fire top up paytm
    free fire diamond topup
    free fire diamond top up
    top up of free fire
    free fire top up site
    top up for free fire
    free fire top up shop
    free fire top up website
    garena top up centre free fire
    garena ff id top up
    garena top up diamond
    free fire game top up
    free fire top up offers
    free fire diamond store
    coda shop diamond ff
    free fire top up link
    free fire top up live
    free fire top up websites
    top up app for free fire
    top up app for free fire max
    free fire top up recharge
    free fire top up shop
    free fire top ups
    online free fire top up
    online free fire top up max
    free fire top up apps
    free fire top up diamond
    garena free fire top up website
    free fire airdrop top up online
    free fire monthly membership top up
    free fire diamond top up karo
    diamond top up ff
    seagm free fire
    free fire diamond top up paytm
    free fire account top up
    free fire top up upi
    free fire top up india
    free fire official top up
    top up website free fire
    ggtopup free fire
    seagm free fire top up
    diamond top up website
    ff top up codashop
    free fire diamond top up garena
    seagm free fire diamond
    codashop free fire double diamond
    seagm.com free fire
    garena free fire top up centre
    shop free fire top up
    diamond top up website free fire
    akash game shop free fire top up
    seagm free fire top up india
    seagm free fire india
    winzo top up free fire
    winzo free fire top up
    game shop free fire top up
    free fire top up seagm
    mid shop free fire top up
    codashop garena free fire top up
    ff max top up
    free fire diamond topups
    free fire diamond topup
    seagm ff top up
    garena free fire max top up
    garena free fire max top-up
    free fire max topup
    free fire max top-up
    garena free fire max top up centre
    online free fire max top up
    free fire max top up website
    top up garena free fire max
    garena top up centre codashop
    seagm free fire max
    seagm ff max
    free fire diamonds center
    free fire max game top up
    garena free fire max game top up
    free fire max game top-up
    garena free fire max game top up
    garena free fire max game top-up
    garena free top up
    garena ff top up
    gerena topup ff gamedouble diamond free firedouble diamond top upfree fire double diamonds top up
    top-up garena free fire max
    free fire diamond garena buy
    4000 diamond top up
    app free fire diamond buy
    codashop free fire double diamonds
    free fire rupees top up website
    game top up center ff
    seagm free fire max top up
    free fire diamond top-ups
    free fire diamond top-up
    garena free fire max top up website
    seagm free fire topup
    seagm free fire top up
    winzo free fire top up max
    free fire online top-up
    garena free fire max topup website
    garena free fire max top up website
    game shop free fire top up max
    top up seagm free fire
    game shop free fire max top up
    top-up free fire
    garena free fire max top-up website
    seagm garena free fire top up
    top-up website free fire
    upi free fire top up
    game shop free fire top-up
    free fire top-ups
    top up garena ff
    top up garena free fire max
    top up free fire
    free fire max top-up
    free fire top up
    garena ff diamond top up
    garena free fire top up
    garena free fire max top up

  9. 10 Mesothelioma Tricks All Experts Recommend Mesothelioma settlement

  10. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 3972

  11. WilliamRhype表示:

    https://ciprofloxacin.tech/# buy ciprofloxacin

  12. Ten Myths About Ghost Immobilisers That Aren’t Always True ghost immobiliser For sale

  13. Davidtes表示:

    90 lisinopril lisinopril buy online lisinopril 5

  14. RandallTaulp表示:

    buy cytotec: buy misoprostol over the counter – buy cytotec over the counter

  15. installation表示:

    A Peek In The Secrets Of Window Repair Near Me installation

  16. Davidtes表示:

    rx lisinopril 10mg lisinopril 5 mg brand name 2 lisinopril

  17. Its History Of Audi A3 Key Battery audi car key Cover

  18. Davidtes表示:

    buy cytotec over the counter buy cytotec over the counter cytotec pills buy online

  19. RandallTaulp表示:

    purchase cytotec: cytotec buy online usa – Cytotec 200mcg price

  20. Davidtes表示:

    cost of generic lisinopril lisinopril drug zestril lisinopril

  21. Davidtes表示:

    lisinopril buy online zestoretic medication lisinopril pharmacy online

  22. RandallTaulp表示:

    nolvadex gynecomastia: tamoxifen postmenopausal – aromatase inhibitors tamoxifen

  23. petirtoto表示:

    Thanks a lot for sharing this with all folks you actually understand
    what you’re speaking approximately! Bookmarked.
    Please additionally talk over with my site =).
    We will have a hyperlink exchange contract among us

  24. Ronaldpsymn表示:

    https://lisinopril.network/# can i buy lisinopril over the counter in canada

  25. What Do You Know About Double Glazing Doors Near Me?

    Repairing double glazing windows

  26. Ronaldpsymn表示:

    http://nolvadex.life/# tamoxifen for gynecomastia reviews

  27. Davidtes表示:

    buy cytotec over the counter buy cytotec cytotec online

發佈留言

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