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,229,743 Responses

  1. See What Mesothelioma Litigation Tricks The Celebs Are Using mesothelioma litigation

  2. RandallTaulp表示:

    tamoxifen and osteoporosis: cost of tamoxifen – tamoxifen and bone density

  3. Davidtes表示:

    nolvadex vs clomid how to prevent hair loss while on tamoxifen tamoxifen dosage

  4. RandallTaulp表示:

    lisinopril 10 mg brand name in india: generic zestril – 50mg lisinopril

  5. Davidtes表示:

    buy cytotec over the counter order cytotec online Cytotec 200mcg price

  6. Ronaldpsymn表示:

    http://nolvadex.life/# who should take tamoxifen

  7. Davidtes表示:

    cipro buy cipro online ciprofloxacin over the counter

  8. RandallTaulp表示:

    buy cipro: cipro pharmacy – antibiotics cipro

  9. WilliamRhype表示:

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

  10. Davidtes表示:

    buy ciprofloxacin over the counter ciprofloxacin mail online antibiotics cipro

  11. Side By Side Fridge Freezer Under Counter: What No One Is Talking
    About which brand has the best side by side Refrigerator

  12. RandallTaulp表示:

    cytotec buy online usa: cytotec pills buy online – buy misoprostol over the counter

  13. Ronaldpsymn表示:

    http://nolvadex.life/# effexor and tamoxifen

  14. Davidtes表示:

    cheap propecia without rx buy generic propecia without a prescription propecia generics

  15. Davidtes表示:

    price of zestril 30 mg lisinopril pills lisinopril 20 mg buy

  16. RandallTaulp表示:

    lisinopril 10 mg tablet price: lisinopril 419 – lisinopril online pharmacy

  17. Hi, i read your blog occasionally and i own a similar one and
    i was just curious if you get a lot of spam remarks?
    If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me mad so any assistance is very much appreciated.

  18. Davidtes表示:

    where can i buy cipro online cipro ciprofloxacin buy cipro cheap

  19. Ronaldpsymn表示:

    http://lisinopril.network/# zestril drug

  20. Davidtes表示:

    tamoxifen and osteoporosis tamoxifen buy tamoxifen and bone density

  21. jamuslot表示:

    Thɑnk you for the good writeup. It in truth was a leisure account it.

    Look advanced too more Ьrought agreeable from you!
    However, how could we keep in tоucһ?

    Feel free to visit my weƅ-site: jamuslot

  22. You’ll Be Unable To Guess Dreme L30’s Benefits dreme l30 (http://www.tanzlife.co.Tz)

  23. Davidtes表示:

    nolvadex steroids tamoxifen for gynecomastia reviews cost of tamoxifen

  24. RandallTaulp表示:

    cytotec abortion pill: Misoprostol 200 mg buy online – Misoprostol 200 mg buy online

  25. RandallTaulp表示:

    buy cytotec over the counter: cytotec pills buy online – buy cytotec over the counter

  26. A Step-By’-Step Guide For Leeds Door And Window upvc Door Repairs Roundhay

  27. Five Killer Quora Answers On Robot Vacuum Empty Itself Robot Vacuum Empty Itself

  28. Ronaldpsymn表示:

    http://cytotec.club/# buy cytotec over the counter

  29. Robot Vacuum Best Explained In Fewer Than 140 Characters robot mop and Vacuum combo

發佈留言

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