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,523,821 Responses

  1. xxx porn表示:

    I read this post fully about the difference of latest and preceding technologies,
    it’s amazing article.

  2. mostbet_jlpa表示:

    mostbet официальный сайт mostbet официальный сайт .

  3. Charleskepsy表示:

    canada pharmacy not requiring prescription https://xxlmexicanpharm.com/# mexico drug stores pharmacies

  4. PENIPU表示:

    Hello there! Do you know if they make any plugins to help with SEO?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Kudos!

  5. Davidnof表示:

    dark market 2025 https://github.com/darkwebwebsites/darkwebwebsites – darknet drug store darkmarket list

  6. MichaelGeony表示:

    pharmacy no prescription required https://discountdrugmart.pro/# discount drug pharmacy

  7. bokep表示:

    With havin so much written content do you ever run into any issues of plagorism or copyright violation?
    My blog has a lot of exclusive content I’ve either created myself or outsourced but it appears a
    lot of it is popping it up all over the internet without
    my authorization. Do you know any techniques to help protect against content
    from being ripped off? I’d genuinely appreciate it.

  8. mostbet_anpa表示:

    ставки на спорт бишкек http://www.gtrtt.com.kg/ .

  9. Salt trick表示:

    It’s going to be finish of mine day, except before end I am reading this fantastic piece of writing to improve my experience.

  10. How To Solve Issues Related To Door Fitter Bromley repair sash windows bromley

  11. coba777表示:

    Why users still use to read news papers when in this technological world the whole thing is existing on net?

  12. sohuxxx表示:

    Hell arre using WordPress foor your ste platform?

    I’m neww to thee bloog wold but I’m trying to get started and crewate my own. Do you eed anny
    coding expertise too maoe your owwn blog?

    Any hwlp woulld be really appreciated!

  13. Rodneytus表示:

    tor dark web https://github.com/darkwebmarketslinks/darkwebmarkets – darknet seiten tor marketplace

  14. MarkWat表示:

    Всем привет! Подскажите, где почитатьполезные блоги о займах? Пока нашел – finanexpert.ru

  15. MichaelIncap表示:

    darknet marketplace https://github.com/darknetmarketslinks/darknetmarketlinks – darkmarket dark web market

  16. WillieRap表示:

    canadian pharmacy mall: canada pharmacy online – easy canadian pharm

  17. Porterfax表示:

    online pharmacy delivery usa online pharmacy delivery usa Best online pharmacy

  18. MichaelGeony表示:

    pharmacy discount coupons https://easycanadianpharm.com/# canadian pharmacy online reviews

  19. Quired for Light Equipment Abscmbly.

  20. Buy Viagra, Cialis or another medication for ED quick and safe at Pharmacie. Australie pharmacie en ligne tadalafil vente sans ordonnance sildenafil levitra. Combien coute le cialis 5mg acheter sildenafil biogaran pharmacie ligne. Viagra en ligne, ordre en ligne viagra, Conferencedesbatonniers. Generique yasmin pharmacie en ligne en france achat.Yasmin comprime. Viagra sans prescription, viagra en pharmacie, Nikobleach. Commande de viagra, viagra en canada, Nousiainen Pharmacie en. Viagra avec une remise, viagra suisse pharmacie, Controleng. https://eurogeneriqueetfrance.com/spiractin/ Generique Spiractin Pilule

  21. 9 Signs That You’re The Male Masturbation Sex Toys Expert male Masturbation Toy (http://Www.Diggerslist.com)

  22. Thể thao điện tử này có hệ thống công nghệ quản lý khá cao,
    đảm bảo tốc độ trò chơi diễn ra ổn định.

發佈留言

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