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,147,778 Responses

  1. 10 Life Lessons That We Can Learn From Double Glazed Units Near Me replacement double Glazed units near me

  2. The Reasons To Focus On Improving Jaguar Keys Replacement Jaguar key replacement cost

  3. 5 Clarifications Regarding Best Online Shopping Sites Clothes 4
    Pair Baby Socks Set – https://Vimeo.com/,

  4. Ten Sweet Bonanza Xmas Slot Demo That Will Actually Make Your Life Better Main akun demo Slot sweet bonanza

  5. Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed surfing around your blog posts.
    In any case I’ll be subscribing to your feed and I hope you write again soon!

  6. Mobile slots表示:

    Why Interactive Slots Is Still Relevant In 2023 Mobile slots

  7. Get To Know You The Steve Jobs Of The Car Key Lockout Service Industry Key
    unlock service (https://losguerrerosdeoracion.com/)

  8. Os melhores jogos deste cassino Blaze

  9. 11 Methods To Completely Defeat Your Best Slots Modern Slots (http://Www.Wakewiki.De)

  10. Medicament information. What side effects can this medication cause?
    can i get trileptal online
    Best information about medication. Read now.

  11. “The Railroad Injuries Litigation Awards: The Top, Worst, Or Weirdest Things We’ve Ever Seen railroad Injuries lawyer

  12. Cheapest Online Grocery Shopping Uk: What No One Is Talking
    About Exterior Construction Glue

  13. The 12 Most Unpleasant Types Of High Limit Slots Accounts You Follow On Twitter Evoplay Slots Payouts

  14. What Freud Can Teach Us About Novice Slots demo Slots – http://www.tradwicca.hu

  15. Helpful knowledge Kudos.

  16. Could Trusted Online Shopping Sites For Clothes Be The Key To 2023’s Resolving?
    fall arrest d-Ring connectors

  17. slot demos表示:

    Ten Slot Demos That Really Change Your Life slot demos

  18. vimeo.Com表示:

    Solutions To The Problems Of Online Famous Shopping Sites vimeo.Com

  19. Pete表示:

    20 Reasons Why Ai To Rewrite Text Will Not Be Forgotten ai article
    rewriter (Pete)

  20. There is definately a great deal to find out about this issue.

    I love all the points you have made.

  21. Evelyn表示:

    The Ultimate Guide To Novice Slots demo slots (Evelyn)

  22. Qooh.Me表示:

    Ask Me Anything: 10 Responses To Your Questions About Slots For Fun Best Online
    Slots (Qooh.Me)

  23. 20 Up-Andcomers To Watch The Medium Variance Slots Industry Evoplay slots fun

  24. 10 Things You’ve Learned From Kindergarden To Help You Get Started With Shopping Online Black Ethernet Cable 10 Feet

  25. Vimeo.Com表示:

    Learn About How To Ship To Ireland From Uk While Working From The Comfort Of Your Home Vimeo.Com

  26. kiddy porn表示:

    Does your site have a contact page? I’m having problems locating it but, I’d like to shoot
    you an email. I’ve got some creative ideas for your blog
    you might be interested in hearing. Either way, great website and I look
    forward to seeing it expand over time.

  27. proscar表示:

    Medicament information for patients. Generic Name.
    proscar
    Some trends of medicines. Read now.

  28. The Best Advice You Can Ever Receive On Semi Truck Lawsuit semi truck accident lawyer

  29. free onlyfans表示:

    Thanks for finally writing about > JS 依照元素出現數量排序 – 馬老師 雲端研究室 < Loved it!

發佈留言

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