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

  1. Swap Pro Air表示:

    Unquestionably imagine that that you said. Your favorite justification appeared to be on the
    web the simplest thing to take into accout of. I say to you, I definitely
    get irked while people consider concerns that they plainly do not understand
    about. You managed to hit the nail upon the top as neatly
    as outlined out the entire thing with no need
    side effect , folks can take a signal. Will likely be
    again to get more. Thank you

  2. Z-Star表示:

    Hello my family member! I want to say that this post is amazing,
    nice written and come with approximately all vital infos.
    I would like to peer more posts like this .

  3. Good day! I could have sworn I’ve been to this site before but
    after browsing through some of the post I realized it’s new to me.
    Anyways, I’m definitely happy I found it and I’ll be book-marking and
    checking back often!

  4. Lestersarly表示:

    купить алкоголь на дом доставка алкоголя москва

  5. CurtisBam表示:

    Indian Cert Pharm: indian pharmacy – Indian Cert Pharm

  6. Brandonnot表示:

    Best Indian pharmacy: Indian pharmacy that ships to usa – Best online Indian pharmacy

  7. Hi! I could have sworn I’ve been to this blog before but after reading through some of the post I realized it’s new to me.
    Anyways, I’m definitely glad I found it and I’ll be
    bookmarking and checking back often! https://teramus.gr/index.php/services/item/20-building

  8. Bromley Window Repair: It’s Not As Expensive As You Think Double Glazing Window Repair Bromley

  9. mitolyn表示:

    This website really has all the information I needed concerning this subject and didn’t know who to ask.

  10. Seriously lots of excellent advice!

  11. magnificent issues altogether, you simply won a new reader.
    What could you suggest about your publish that you simply
    made a few days ago? Any positive?

  12. Beneficial knowledge Appreciate it.

  13. для вывода из запоя в стационаре https://vyvod-iz-zapoya-v-stacionare21.ru .

  14. seo London表示:

    8 Tips For Boosting Your SEO Specialist London Game seo London

  15. Astonishingly individual pleasant website. Great details available on couple of clicks on.
    https://bielsko.biala.pl/

  16. Brandonnot表示:

    Online medicine: Best Indian pharmacy – Indian Cert Pharm

  17. вывод из запоя в стационаре воронежа http://vyvod-iz-zapoya-v-stacionare-voronezh24.ru .

  18. CurtisBam表示:

    canadian pharmacy 24: Canadian Md Pharm – legit canadian pharmacy

  19. Antoniofep表示:

    Bet Zula, spor bahisleri konusunda essiz deneyimler sunar. Fenerbahce Galatasaray derbisi icin Betzula giris yaparak favori tak?mlar?n?za destek olabilirsiniz.

    Betzula’n?n mobil uyumlu tasar?m?, kullan?c?lar?na her zaman kolayl?k saglar. guncel duyurular? kac?rmadan yeni kampanyalardan haberdar olabilirsiniz.

    Fenerbahce ve Galatasaray gibi buyuk tak?mlar?n heyecan?n? Betzula ile yasayabilirsiniz.

    Ayr?ca, bet zula giris linki, mobil cihazlar uzerinden kolay erisim sunar. Ozel olarak, https://apsistek.com/yeni-giris/ – bet zula giris, kolay ve h?zl? giris imkan?.

    Betzula, mobil uyumlu ve h?zl? erisim f?rsatlar?na kadar tum kullan?c?lar?n ihtiyaclar?n? kars?lar. favori tak?m?n?z?n galibiyetini kutlamak icin hemen Betzula’ya kat?l?n!
    707707+

  20. CurtisBam表示:

    online pharmacy canada: Canadian Md Pharm – canadian mail order pharmacy

  21. Brandonnot表示:

    Mexican Easy Pharm: Mexican Easy Pharm – Mexican Easy Pharm

  22. CurtisBam表示:

    mexico drug stores pharmacies: mexican rx online – Mexican Easy Pharm

  23. bokep sma表示:

    Helpful information. Lucky me I found your site by accident, and
    I am surprised why this accident didn’t happened in advance!

    I bookmarked it.

  24. Brandonnot表示:

    Mexican Easy Pharm: mexican border pharmacies shipping to usa – pharmacies in mexico that ship to usa

  25. Brandonnot表示:

    pharmacy wholesalers canada: CanadianMdPharm – legitimate canadian online pharmacies

  26. CurtisBam表示:

    medication from mexico pharmacy: reputable mexican pharmacies online – Mexican Easy Pharm

發佈留言

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