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,467,153 Responses

  1. 14 Questions You’re Anxious To Ask Best Brands For Refrigerators Refrigeration Brands

  2. Faustino表示:

    Hello there! I know this is kinda off topic but I was wondering which blog platform are you using
    for this website? I’m getting fed up of WordPress
    because I’ve had issues with hackers and I’m looking at alternatives for another
    platform. I would be awesome if you could point
    me in the direction of a good platform.

  3. BradleyEdino表示:

    yat?r?ms?z deneme bonusu veren siteler deneme bonusu veren siteler yeni deneme bonusu veren siteler yeni

  4. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# deneme bonusu veren yeni siteler

  5. Wallacefex表示:

    guvenilir casino siteleri: canl? casino siteleri – canlД± casino bonusu veren siteler

  6. Appreciate the insightful article. Find more at عمل پی ار کی

  7. 평택호빠表示:

    일상이 지겨우신가요? 신선함을 느끼고 싶다면 평택호빠로 놀러오세요

  8. JasonUnsoG表示:

    https://slotsiteleri25.com/# guvenilir slot siteleri

  9. Jordan Sutton表示:

    Flood recovery requires specialized skills—get professional help from water damage cleanup if you’re affected

  10. 10 Expressions To Avoid In Sales Communication 에볼루션 베팅취소

  11. BradleyEdino表示:

    deneme bonusu veren casino siteleri guvenilir casino siteleri deneme bonusu veren casino siteleri

  12. FrankEmine表示:

    sweet bonanza giris: sweet bonanza guncel – sweet bonanza demo oyna

  13. 10 Top Facebook Pages Of All Time About Truck Accident Attorneys
    lawyer truck Accident – https://www.metooo.io,

  14. webcam girls表示:

    I have been exploring for a bit for any high-quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i?m happy to convey that I’ve a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to do not forget this web site and give it a look regularly.

  15. JasonUnsoG表示:

    https://denemebonusuverensiteler25.com/# yat?r?ms?z deneme bonusu veren siteler

  16. Wallacefex表示:

    Casino Siteleri: casino bahis siteleri – Casino Siteleri

  17. In some scenarios, it might be best to place pet dogs in a
    little bedroom and close the door.

  18. Fixed-income funds are made up of government and corporate securities that provide a fixed return and are usually low risk.

  19. I absolutely love your blog and find the majority of your post’s to be just what I’m looking
    for. can you offer guest writers to write content
    for you? I wouldn’t mind producing a post or elaborating on a
    lot of the subjects you write with regards to here.
    Again, awesome website!

  20. Good response in return of this matter with real arguments and describing all regarding that.

  21. The goal is to locate commonalities and get to
    an arrangement without going to court.

  22. You actually make it seem so easy with your presentation but I find this topic to be really something that I think I
    would never understand. It seems too complex
    and very broad for me. I’m looking forward for your next
    post, I’ll try to get the hang of it!

  23. You’ll Be Unable To Guess Patio Doors Repair Near Me’s Secrets patio doors repair near me (trade-britanica.trade)

  24. Dannyfut表示:

    tor dark web onion market dark markets

  25. Edith Herrera表示:

    This was a fantastic read. Check out queen and slim Mural for more

發佈留言

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