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

  1. LarryCoell表示:

    ultrabet tr online ultrabet giris ultrabet yeni giris 1125

  2. Hiya very nice website!! Man .. Beautiful .. Superb ..
    I will bookmark your website and take the feeds additionally?
    I’m glad to seek out numerous helpful info here within the post,
    we want work out more strategies on this regard, thanks for sharing.
    . . . . .

  3. Ernest表示:

    9 Things Your Parents Taught You About French Door
    Style Fridge Freezer french door style fridge (Ernest)

  4. Derekfar表示:

    ultrabet: ultrabet – ultrabet guncel

  5. HassanbuifS表示:

    https://slot-tr.online/# slot oyunlar? puf noktalar?
    deneme bonusu veren yeni siteler

  6. LarryCoell表示:

    deneme bonusu veren yeni siteler deneme bonusu veren siteler betturkey deneme bonusu veren siteler betturkey

  7. My brother suggested I may like this web site.
    He was once totally right. This put up truly made my day.
    You can not imagine simply how much time I had spent for this info!
    Thanks!

  8. 10 Ways To Create Your Get An ADHD Diagnosis Empire how do you diagnose adhd in adults

  9. Sazrmlg表示:

    Всё о покупке аттестата о среднем образовании: полезные советы

    ou812chat.com/read-blog/4619

  10. What’s The Reason You’re Failing At Upvc Window Handle Replacement upvc Bay window

  11. CarrollUnton表示:

    denemebonusuverensiteler.top https://casinositeleri.win/# Casino Siteleri

  12. AtmlHesia表示:

    Purchase medications at our online drugstore at stromectol tablets for your next medicine purchase.

  13. Derekfar表示:

    denemebonusuverensiteler.top: deneme bonusu veren siteler denemebonusu2026.com – deneme bonusu veren yeni siteler

  14. My partner and I stumbled over here by a different
    website and thought I should check things out. I
    like what I see so now i am following you. Look forward
    to looking at your web page repeatedly.

  15. Derekfar表示:

    slot oyunlar? puf noktalar?: az parayla cok kazandiran slot oyunlar? – slot tr online

  16. anonymous viewing of instagram stories http://freeinstviewer.com .

  17. 123fox表示:

    Hello, yup this paragraph is actually good and I have learned
    lot of things from it about blogging. thanks.

  18. CarrollUnton表示:

    deneme bonusu veren siteler betturkey https://ultrabet-tr.online/# ultrabet yeni giris 1125

  19. 5 ADHD Diagnosis UK Projects For Every Budget adhd How To Get diagnosed

  20. Sazrkmf表示:

    Легальная покупка диплома о среднем образовании в Москве и регионах
    octomo.co.uk/read-blog/3013

  21. CarrollUnton表示:

    deneme bonusu veren siteler denemebonusu2026.com https://slot-tr.online/# slot tr online

  22. LarryCoell表示:

    matadorbet giris matadorbet giris matadorbet bid

發佈留言

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