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,228,714 Responses

  1. LarryCoell表示:

    Casino Siteleri casino siteleri win Casino Siteleri

  2. Derekfar表示:

    deneme bonusu veren siteler yerliarama.org: deneme bonusu veren siteler betturkey – deneme bonusu veren yeni siteler

  3. Hi there, I read your blog daily. Your humoristic style is awesome, keep up the good work!

  4. Sylvia表示:

    Why No One Cares About Mesothelioma Law Firms mesothelioma
    compensation (Sylvia)

  5. Maggie表示:

    Unexpected Business Strategies Helped Key Ignition Replacement Succeed car key ignition replacement near me –
    Maggie,

  6. Derekfar表示:

    matadorbet: matadorbet.bid – matadorbet.bid

  7. king-wifi.win表示:

    Reasons The Latest Kindle 3G Use Massage Chairs
    충장오피 (king-wifi.win)

  8. Diplomi_hmkn表示:

    купить диплом задним числом купить диплом задним числом .

  9. ClydePab表示:

    slot oyunlar? puf noktalar?: en kazancl? slot oyunlar? – slot siteleri

  10. LarryCoell表示:

    denemebonusuverensiteler.top denemebonusuverensiteler.top deneme bonusu veren siteler 2024

  11. Chauncey表示:

    Auto Door Lock Repair: 10 Things I Wish I’d Known Earlier car central locking system
    repair near me (Chauncey)

  12. Saab Car Keys表示:

    7 Helpful Tricks To Making The Most Of Your Saab Keys Saab Car Keys

  13. Hiya very cool web site!! Guy .. Beautiful ..
    Superb .. I’ll bookmark your web site and take the feeds also?
    I’m satisfied to find a lot of helpful info here within the submit, we want
    work out more techniques on this regard, thank you for sharing.
    . . . . .

  14. Buzzwords De-Buzzed: 10 Different Ways Of Saying Electric
    Wheelchair Lightweight electric wheelchair

  15. Diplomi_orMl表示:

    купить свидетельство об усыновлении many-diplom77.ru .

  16. HassanbuifS表示:

    https://matadorbet.bid/# matadorbet.bid
    deneme bonusu veren siteler yerliarama.org

  17. I have read a few just right stuff here. Certainly price bookmarking for revisiting.
    I surprise how a lot attempt you set to create this kind of magnificent
    informative website.

  18. What Is Item Upgrader And How To Utilize It
    upgrade Item (https://longshots.wiki/)

  19. 10 Locations Where You Can Find Coffee Grinder ceramic Grinder

  20. ClydePab表示:

    Deneme Bonusu Veren Siteler: Casino Siteleri – Canl? Casino Siteleri

  21. Diplomi_tqMl表示:

    купить настоящий диплом о среднем специальном образовании купить настоящий диплом о среднем специальном образовании .

  22. Тут можно преобрести сейф несгораемый купить купить несгораемый сейф

  23. What’s The Current Job Market For Electric Wall Mounted Fires Professionals?
    electric wall mounted Fires

  24. Carin表示:

    What To Say About Asbestos Mesothelioma Lawyers To Your Boss mesothelioma attorneys (Carin)

  25. Thanks a bunch for sharing this with all folks you really recognize what you’re talking approximately!
    Bookmarked. Please additionally talk over with my
    site =). We could have a link trade contract among us

  26. Fantastic Entry Feedback
    Astounding, what a compelling article ! I genuinely enjoyed perusing your insights
    on this matter .
    As a person who has been observing your online
    presence for a period of time , I have to convey that this is alongside your
    best skillfully composed and enthralling content
    to date.
    The way you integrated the viewpoints and scholarly results was genuinely phenomenal.
    I discovered myself as I digested because your
    assertions just seemed to advance astonishingly naturally .

    Feel free to surf to my blog: no kyc casinos

  27. piroozan表示:

    I’m amazed, I have to admit. Rarely do I come across a blog
    that’s both equally educative and entertaining, and
    without a doubt, you have hit the nail on the
    head. The issue is something not enough folks are speaking intelligently about.
    I am very happy that I found this in my hunt for something concerning this.

  28. LarryCoell表示:

    deneme bonusu veren siteler yerliarama.org deneme bonusu veren siteler 2024 deneme bonusu veren siteler denemebonusu2026.com

發佈留言

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