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,245,175 Responses

  1. DarrenUnjuh表示:

    http://cytpremium.com/# buy cytotec over the counter

  2. I believe this is one of the such a lot vital info for me.
    And i’m satisfied reading your article. However wanna statement on few common things,
    The website style is perfect, the articles is really excellent : D.

    Just right activity, cheers

  3. Drug information. What side effects?
    buying fosamax pill
    All information about pills. Read now.

  4. BrandenSlomy表示:

    how can i get generic clomid without prescription: cheap clomid – clomid prices

  5. Узнай все о операция на исправление перегородки носасептопластика носа в москве

  6. pepek表示:

    I’ve been surfing online greater than three hours as of late, but I by no means discovered any fascinating article like yours.
    It’s beautiful price enough for me. Personally, if all site owners and
    bloggers made just right content material as you probably did, the internet will likely
    be much more helpful than ever before.

  7. вывод из запоя стационар краснодар family2.quadrobb.me/viewtopic.php?id=1838 .

  8. BernardMop表示:

    Lisinopril 1st cheapest Lisinopril buy Lisinopril online

  9. see here now表示:

    Yesterday, while I was at work, my cousin stole my iphone and tested to see if
    it can survive a twenty five foot drop, just so she can be a youtube sensation. My apple ipad is
    now broken and she has 83 views. I know this is entirely off topic but I had to share it
    with someone!

  10. вывод из запоя лечение краснодар http://family2.quadrobb.me/viewtopic.php?id=1838 .

  11. Residential Land scarcity available in City or Town

  12. вывод из запоя на дому краснодар http://mozaisk.anihub.me/viewtopic.php?id=4370 .

  13. BrandenSlomy表示:

    amoxicillin 250 mg price in india: amoxil com pharm – buy amoxicillin from canada

  14. DarrenUnjuh表示:

    https://plavixclo.com/# buy plavix

  15. BernardMop表示:

    buy Lisinopril 1st lisinopril 20 mg canadian pharmacy Lisinopril 1st

  16. AlbertDuM表示:

    cheap plavix antiplatelet drug: buy plavix online – Cost of Plavix without insurance

  17. ABORSI表示:

    Tаnte : Jangan keluar duluuu, tahan sedikit lagii… Mmmhhhhh mmmһhhhhAku
    : Uuuhhhh Iya Tan (Sambil meremɑs dada Tante yg bergoyang)Sudah Tidak tertahankan lagi, lalu Aku merаsa Tante buang air kecil, dan ternyɑta іtu adalah
    ѕaat Tаnte mengalami Orgasme. Kami berdua mengejang hebat, saling berteriaҝ satu ѕama lain, “Αaaaaahhhһh Nunuuuu Aaaahhhh” Seru Tante.
    Aku pun sedikit berteriak кeenakan “Taɑannn aaahhhhh” Crrrooottttt Crooottttt.Τanpa kusadari,
    aku mengeluarkan sрerma di dalam vаgina Tante. Akս pun terkejut, tetapi
    Tante mengatakan bahwa “Udah gpp kok didɑlam, Tante udaһ ga bisa hamil lagi karena pernah ԁi operasi pengangkatan rahim”.
    Hufftttt sontak kata kata itu membuat batin ku menjadi
    lebih tenang.“Nono, km kl mau lagi nanti bilang ke Tante aja ya
    sayang, ga boleh main ini sama Pacar atau Perempuan lain sebelum kamu nikаh yaa…Kalo kɑmu mau tinggal bilang ke Tante yа sayang, Tante ɡamau karena hal ini Nono jadi laki laki bandel nanti, Janji?” Ucap Tante.“Iya Tan,
    Janji kok Nono jg maᥙ nya sama Tante, ҝan sama Tante lebih enak
    hehehe ” Gurau ku kepada Tante“Dasar deehh ponakan Tante
    tersayang, (Tante mengecup bibіrku dengan mesra) Mmmwwahhh Gih mandi, nanti keburu
    mamah papah pada pulang loohh”Ucap Tante.“Okedeh Tan… “Ucaр kuAku tersenyum senyum
    bahagia samƄil melangkahkan kaki ku ke kamar mandi, saat di kamar mandi pun, Aku masih tidak pеrcaya bahwa hal ini benar
    terjadi.

    Look аt my ԝeƅ blog … ABORSI

  18. documento DUA表示:

    Since the admin of this site is working, no
    uncertainty very soon it will be renowned, due to its feature contents.

  19. вывод из запоя цены на дому краснодар https://mozaisk.anihub.me/viewtopic.php?id=4370 .

  20. Узнай все о исправление перегородки лазеромоперация на носовой перегородке

  21. Узнай все о септопластика цена москвасептопластика носа цены в москве

  22. Nikefut表示:

    dark market list darknet market list https://mydarknetmarketlinks.com/

發佈留言

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