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,244,466 Responses

  1. BrandenSlomy表示:

    amoxicillin over the counter in canada: amoxil – generic amoxicillin cost

  2. Hi there it’s me, I am also visiting this website on a regular basis, this
    website is genuinely pleasant and the users are really sharing fastidious thoughts.

  3. If you desire to take a good deal from this piece of
    writing then you have to apply such techniques to your won weblog.

  4. StevenNub表示:

    average cost of prednisone: ray pharm – prednisone tablets canada

  5. JacobLoody表示:

    кухни от производителя — Кухонные гарнитуры напрямую от производителя по выгодным ценам.

  6. 4 Dirty Little Secrets About The All Crypto Casinos Industry
    Crypto casino games

  7. Medicament information. Effects of Drug Abuse.
    buying cheap zithromax for sale
    Actual about drug. Get information here.

  8. Bradleygoath表示:

    dapoxetine online priligy maxpharm cheap priligy

  9. 14 Businesses Doing An Amazing Job At Mesothelioma Attorney Mesothelioma Attorneys

  10. BrandenSlomy表示:

    can you buy clomid without prescription: generic clomid – can i purchase cheap clomid without rx

  11. Margarito表示:

    Five Killer Quora Answers To Indoor Pellet Stove indoor pellet stove – Margarito,

  12. BrandenSlomy表示:

    prednisone 20 mg tablet price: order Prednisone – otc prednisone cream

  13. Bradleygoath表示:

    where can i buy cheap clomid without a prescription generic clomid where to get clomid

  14. StevenNub表示:

    buy dapoxetine online: dapoxetine price – priligy maxpharm

  15. StevenNub表示:

    amoxicillin 500mg without prescription: amoxil – amoxicillin 500mg capsules

  16. gary halbert表示:

    Quality articles or reviews is the important to be a focus for the visitors to pay a visit the web page,
    that’s what this web site is providing.

  17. Тут можно преобрести купить сейф под ружье купить сейф для пистолета

  18. Тут можно преобрести купить оружейный шкаф заказать оружейный сейф

  19. Bradleygoath表示:

    buying cheap clomid without insurance clomid purchase online rex pharm where to get clomid prices

  20. www.myozen.ca表示:

    Your method of explaining all in this article is in fact good, every one can without difficulty be aware of it, Thanks
    a lot https://www.myozen.ca/en/produit/orthotherapy/

  21. Nice blog! Is your theme custom made or did you download it from somewhere?

    A theme like yours with a few simple adjustements would really make my blog shine.
    Please let me know where you got your design. With thanks

  22. StevenNub表示:

    how to get cheap clomid prices: cheap clomid – can i order generic clomid no prescription

  23. Actually when someone doesn’t know then its up to other users that they will assist, so
    here it takes place.

發佈留言

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