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,365,394 Responses

  1. SamuelBix表示:

    how can i get cheap clomid online: buying generic clomid without a prescription – generic clomid without prescription

  2. SamuelBix表示:

    rexall pharmacy amoxicillin 500mg: amoxicillin where to get – buy amoxicillin canada

  3. SmnPaish表示:

    Retail pharmacies in your area are the best place to buy lioresal for sale Online pharmaci

  4. ThelmaFange表示:

    On our website you will find a unique assortment valuable coins from all over the world. We offer both modern and historical specimens that will become real finds for lovers of numismatics.
    For more information, follow the link [url=https://numizmatexpert.ru/]монеты ссср цены [/url]
    Our team of experts carefully selects each coin, ensuring high quality.

  5. KennethPah表示:

    ciprofloxacin over the counter ci pharm delivery cipro ciprofloxacin

  6. Nikefut表示:

    darknet drug links dark web websites tor dark web

  7. You’ll Never Be Able To Figure Out This Coffee Bean Coffee Machine’s Tricks Coffee Bean Coffee Machine

  8. Здесь можно домашние сейфы купить сейф для дома купить

  9. WΟW just what I wass searching for. Camme here by sesarching for kliniқ dokter gigi

    my weeb Ƅlog Informasi tambahan

  10. Thanks for sharing your info. I truly appreciate your efforts
    and I am waiting for your further write ups thanks once again.

  11. 9 . What Your Parents Taught You About Car Keys Stolen From House And Car Stolen Car keys
    stolen from house and car stolen – carlocksmith42893.wikigiogio.com

  12. We stumbled over here from a different web page and thought I might as
    well check things out. I like what I see so i am just following you.
    Look forward to looking over your web page repeatedly.

  13. DanielAbice表示:

    https://amoxstar.com/# amoxicillin cephalexin

  14. I was very pleased to discover this website.

    I wanted to thank you for ones time for this wonderful read!!
    I definitely liked every part of it and i also have you book-marked to check out new things on your
    site.

  15. KennethPah表示:

    can you get clomid without dr prescription clomidonpharm buy generic clomid without rx

  16. Excellent beat ! I wish to apprentice while you amend your website, how could i
    subscribe for a weblog website? The account helped me a
    appropriate deal. I had been a little bit acquainted of this your broadcast
    offered brilliant transparent idea http://Blog-kr.dreamhanks.com/question/decouvrez-les-delices-des-tacos-sur-le-boeuf-39/

  17. RobertJar表示:

    prednisone buy cheap: PredniBest – prednisone in canada

  18. Rosalie Casey表示:

    Frisørbesøg er altid en fornøjelse! Hvad med dig? herrrefrisør

  19. foldable pram表示:

    What’s The Job Market For Foldable Pram Professionals Like?
    foldable pram

  20. KennethPah表示:

    cost cheap clomid without insurance clomid no prescription can i order generic clomid without dr prescription

  21. Mike Casey表示:

    Appreciate the detailed insights. For more, visit winning roulette strategies

  22. Andre Peters表示:

    Clean offices lead to happy employees who are more productive cleaning services

  23. hello!,I love your writing so so much! percentage we be
    in contact more about your article on AOL? I require a
    specialist on this area to resolve my problem.

    May be that is you! Taking a look ahead to see you.

發佈留言

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