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,672,001 Responses

  1. Danieljek表示:

    can you buy amoxicillin over the counter https://amoxilcompharm.com/# amoxicillin 500mg pill

  2. StevenNub表示:

    prednisone capsules: generic Prednisone – buy prednisone without prescription paypal

  3. idplug.biz 表示:

    You really make it appear so easy along with your presentation but I in finding this matter
    to be actually one thing which I feel I would never understand.
    It sort of feels too complex and extremely huge for me.

    I am having a look ahead for your subsequent submit,
    I will try to get the dangle of it!

  4. Blakesmema表示:

    https://www.tort-dubr.ru — Посетите наш сайт для получения подробной информации.

  5. BrandenSlomy表示:

    cost of amoxicillin 875 mg: Amoxicillin for sale – amoxicillin in india

  6. Bradleygoath表示:

    prednisone 1 mg for sale prednisone ray pharm prednisone where can i buy

  7. whoah this blog іs wonderful і ⅼike studying your posts.

    Stay սp the great wօrk! You understand, lоtѕ of people are loοking round foг this informatіon, yοu
    can aid them greаtly.

    Check out mу web site :: stashpatricks cc

  8. StevenNub表示:

    buy priligy max pharm: Priligy tablets – cheap priligy

  9. ขอบคุณสำหรับข้อมูลที่มีประโยชน์มาก จะนำไปปรับใช้ค่ะ

  10. Bradleygoath表示:

    amoxicillin 500mg tablets price in india Com Pharm buy amoxicillin online no prescription

  11. Bradleygoath表示:

    amoxicillin 800 mg price Com Pharm where can i buy amoxicillin without prec

  12. Hi there, You’ve done an excellent job. I’ll certainly digg it and personally
    suggest to my friends. I am confident they will be benefited from this web site.

  13. StevenNub表示:

    priligy: cheap priligy – cheap priligy

  14. Danieljek表示:

    amoxicillin 500 mg for sale https://amoxilcompharm.com/# buy amoxicillin 500mg

  15. SubPaish表示:

    सट्टेबाजी के खेल असली पैसे के खेल असली पैसे वाले कैसीनो

  16. This information is priceless. How can I find out more?

發佈留言

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