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,246,590 Responses

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

  2. Tɑnte : Jɑngan keluar duluuu, tаhan sedikit lagii… Mmmhhһhh mmmһhhhhAku
    : Uuuhhhh Iуa Tan (Sambil meremas dada Tante yg bergoyang)Sudah
    Tidak tertahankan ⅼagі, lalu Aku merasa Tɑnte buang aіr
    kecil, dan ternyata itu adalah saat Tantе mengаlami
    Orgasme. Kami berduа mеngejang hebat, saling berteriak satu sama lain,
    “Aaaaaahhhhh Nunuᥙuu Aaaahhhh” Seru Tante. Aku pun seԀikit bertеriak keenakan “Taɑannn aaɑhhhhh” Crrrooottttt Ϲrooottttt.Tanpa kusadaгi,
    aku mengeluarkan sperma di dalam vagina Tante. Aku pun terkejᥙt,
    tetapi Tante mengatakɑn bahwa “Udah ցpp kok didalam, Tante udah ga
    bisa hamil lagi karena pernah di operasi
    pengangkatan rahim”. Hսfftttt sontak kata kata itu membսat batin ku menjadi lebih tenang.“Nono, km kl mau lagi nanti bilɑng ke
    Tante aja ya sayang, ga boleh main ini sama Pacar atau Perempuɑn ⅼain sebelum kamu nikah yaa…Kɑlo kamu mau tinggal bilang ke Tantе ya sayang,
    Tante gamɑu karena hal ini Nono jadi laki lakі bandel nanti, Janji?” Ucap Tante.“Iya Tan, Janji kok
    Nono jg mau nya sama Tante, kan sama Tante lebih enak hehehe ” Gurau ku
    kepada Tante“Dasar deehh ponakan Tante tersayang, (Tante mengecup bibirku dengan mesra) Mmmwwahhh Gih mandi, nanti keburu mamaһ
    papah pada pulang loⲟhh”Ucap Τante.“Okеdeh Tan… “Ucap kᥙAku tersenyum senyum bahagia sambil melangkahkan ҝaki ku
    ke kamar mandi, sɑat di kamar mandi pun, Aku masih tidak perсaya bahwa hal
    ini benar terjadi.

    Also visit my web page: BOKEP INDONESIA

  3. hello there and thank you for your information –
    I’ve definitely picked up something new from right here.

    I did however expertise several technical issues using this web site, as I experienced to reload the web site a
    lot of times previous to I could get it to load properly.
    I had been wondering if your web host is OK? Not that I’m complaining, but slow loading instances times will sometimes
    affect your placement in google and can damage your high quality
    score if advertising and marketing with Adwords. Well I am
    adding this RSS to my email and could look out for a lot more of your respective interesting
    content. Make sure you update this again soon.

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

  5. продамус промокод скидка https://www.vip.rolevaya.info/viewtopic.php?id=710 .

  6. Great work! This is the type of information that should be shared around the web.
    Disgrace on Google for no longer positioning this post upper!

    Come on over and visit my site . Thank you =)

  7. BrandenSlomy表示:

    priligy max pharm: priligy max pharm – priligy

  8. Akiko Pizzaro表示:

    Higly Demand of Durian Land

  9. Definitely believe that which you said. Your favorite reason appeared to be
    on the internet the simplest thing to be aware of.
    I say to you, I definitely get annoyed while people think about worries that they plainly do not know
    about. You managed to hit the nail upon the top and also defined out the
    whole thing without having side effect , people could
    take a signal. Will probably be back to get more. Thanks

  10. I recently had my screen replaced, and I was amazed at how quickly the repair was done! If you’re looking for reliable phone repair services, check out battery repair for great tips and support

  11. Hmm it appears like your blog ate my first comment (it was super long) so I
    guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.

    I too am an aspiring blog blogger but I’m still new to everything.
    Do you have any suggestions for beginner blog writers?
    I’d definitely appreciate it.

  12. I know this web page gives quality dependent content and extra data, is there any other website which
    provides these stuff in quality?

    my blog post … فروشگاه آلات موسیقی ساز آرت

  13. BrandenSlomy表示:

    amoxicillin in india: Com Pharm – buy amoxicillin online without prescription

  14. TradeGPT表示:

    I’ll immediately grab your rss as I can’t find your e-mail subscription link or newsletter service.
    Do you have any? Kindly permit me realize so
    that I could subscribe. Thanks.

  15. BernardMop表示:

    Lisinopril 1st lisinopril 50 mg price buy Lisinopril 1st

  16. Your mode of describing everything in this paragraph is truly fastidious, all
    be able to effortlessly know it, Thanks a lot https://www.provenexpert.com/es-es/pub022/

  17. AlbertDuM表示:

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

發佈留言

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