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,143,134 Responses

  1. The Three Greatest Moments In Tommy Hilfiger And
    Bags History tommy Hilfiger Bags usa

  2. Vimeo表示:

    10 Facts About Birth Defect Lawsuit That Insists On Putting You In An Optimistic Mood Vimeo

  3. ElmerWes表示:

    Kamagra 100mg buy kamagra online sildenafil oral jelly 100mg kamagra

  4. The No. Question That Everyone In Semi Truck Must Know How To Answer elk city Semi truck accident lawyer

  5. 11 Ways To Completely Revamp Your Washing Machine 12kg Price
    washing machine 12kg Price

  6. CharlesDog表示:

    http://viagras.online/# buy Viagra over the counter

  7. Dermandar.Com表示:

    What’s The Current Job Market For Window Doctor Near
    Me Professionals Like? Window Doctor Near Me (Dermandar.Com)

  8. TylerPhona表示:

    https://levitrav.store/# Levitra 20 mg for sale

  9. spam表示:

    5 Joker123 Gaming Instructions From The Pros spam

  10. JamesNow表示:

    Buy Cialis online: Cialis 20mg price in USA – Cheap Cialis

  11. muat表示:

    Asking questions are in fact pleasant thing if you are not understanding
    anything entirely, except this piece of writing
    provides nice understanding even.

  12. Source表示:

    My brother recommended I might like this website. He was totally right. This post actually made my day. You can not imagine simply how much time I had spent for this information! Thanks!

  13. synoshi表示:

    I was recommended this blog by my cousin. I’m not sure
    whether this post is written by him as no one else
    know such detailed about my problem. You’re incredible! Thanks!

  14. TylerPhona表示:

    http://levitrav.store/# Buy Vardenafil 20mg online

  15. 16 Must-Follow Instagram Pages For CSGO New Case-Related Businesses case clutch (http://www.jzq5.cn/Space-uid-205043.html)

  16. Tips For Explaining Autolocksmiths To Your Boss Locksmith Auto keys

  17. Claude表示:

    20 Up-Andcomers To Watch The Bunk Bed Price Uk Industry bunk beds stores (Claude)

  18. This Is A Folding Scooters Success Story You’ll Never Be
    Able To Foldable Mobility Scooter With Spacious Seat
    And Extended Range (http://Www.Mymobilityscooters.Uk)

  19. Vimeo.Com表示:

    Why Everyone Is Talking About Medical Malpractice Lawyer Right Now Vimeo.Com

  20. TylerPhona表示:

    https://cialist.pro/# Cialis 20mg price in USA

  21. ElmerWes表示:

    sildenafil oral jelly 100mg kamagra kamagra pills buy kamagra online usa

  22. site表示:

    https://natumbe.ru
    Hey there this is somewhat of off topic but
    I was wondering if blogs use WYSIWYG editors or if you have
    to manually code with HTML. I’m starting a blog soon but have no coding
    experience so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  23. ElmerWes表示:

    buy kamagra online usa cheap kamagra Kamagra Oral Jelly

  24. JamesNow表示:

    cheapest cialis: buy cialis overseas – Buy Tadalafil 10mg

  25. JamesNow表示:

    Kamagra 100mg price: kamagra pills – super kamagra

  26. JamesNow表示:

    Buy Cenforce 100mg Online: order cenforce – cheapest cenforce

  27. 12 Facts About Upvc Repairs That Will Make You Think Twice About The Cooler.
    Cooler upvc Windows repair near me

  28. CharlesDog表示:

    http://viagras.online/# sildenafil over the counter

  29. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?

  30. GeorgeSep表示:

    buy Viagra over the counter: Buy Viagra online – Generic Viagra online

發佈留言

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