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,368,266 Responses

  1. It’s great that you are getting thoughts from this post as well as from our argument
    made at this time.

  2. RamonRek表示:

    ozempic cost: buy ozempic – buy ozempic

  3. penipu表示:

    A motivating discussion is worth comment. There’s no doubt that that
    you need to write more about this topic, it might not be a taboo subject but generally folks don’t discuss these topics.
    To the next! Best wishes!!

  4. HaroldBig表示:

    rybelsus coupon: rybelsus cost – semaglutide tablets

  5. You need to take part in a contest for one of the highest quality websites on the
    internet. I am going to highly recommend this blog!

  6. RamonRek表示:

    semaglutide online: buy rybelsus online – buy semaglutide pills

  7. It’s The Good And Bad About Private ADHD Assessment How To Get A Private Adhd Assessment

  8. Сервисный центр предлагает ремонт телефонов archos адреса качественый ремонт телефона archos

  9. BarryDof表示:

    https://ozempic.art/# Ozempic without insurance

  10. Asking questions are truly nice thing if you are not understanding anything totally, however this
    piece of writing offers pleasant understanding yet.

    Feel free to surf to my web blog Mostbet Polska

  11. Frankevild表示:

    http://rybelsus.shop/# semaglutide cost

  12. HaroldBig表示:

    buy cheap ozempic: buy ozempic pills online – buy cheap ozempic

  13. Jimmie表示:

    For the reason that the admin of this website
    is working, no uncertainty very rapidly it will be well-known, due to its quality contents.

  14. اویل کش表示:

    اویل کش (Oil Catch Can) یک قطعه مهم در سیستم‌های موتور است که به منظور
    جلوگیری از ورود بخارات روغن و گازهای غیرسوختی به سیستم ورودی هوا و
    محفظه احتراق طراحی شده است. این دستگاه به‌خصوص در
    خودروهای با موتورهای قدرتمند،
    تقویت‌شده و مسابقه‌ای
    مورد استفاده قرار می‌گیرد. در ادامه به بررسی جزئیات کامل اویل
    کش، نحوه عملکرد، مزایا، معایب و کاربردهای آن خواهیم پرداخت.

    نوربرت پرفورمنس

  15. 10 Healthy Habits For A Healthy Chimineas For Sale chimineas near
    Me (Prbookmarkingwebsites.com)

  16. BarryDof表示:

    https://rybelsus.shop/# semaglutide tablets

  17. slot2d表示:

    Slot2d adalah 15slot2d.com link untuk daftar akun gacor
    di slot88 resmi tanpa ribet bermain slot gacor online dengan tingkat berkualitas tertinggi di link login daftar slot2d terpopuler deposit murah banjir
    scatter pasti maxwin auto kembali

  18. HaroldBig表示:

    semaglutide online: rybelsus pill – buy semaglutide online

  19. one88表示:

    Hurrah, that’s what I was exploring for, what a stuff!
    existing here at this webpage, thanks admin of this website.

  20. Jeremypag表示:

    rybelsus cost rybelsus coupon semaglutide tablets

  21. RamonRek表示:

    rybelsus price: rybelsus coupon – buy semaglutide pills

  22. bk8表示:

    Hi there! This post couldn’t be written any better! Reading
    this post reminds me of my old room mate! He always kept chatting about this.

    I will forward this article to him. Pretty sure he will
    have a good read. Many thanks for sharing!

  23. Elana表示:

    15 Top Twitter Accounts To Learn More About 2 In 1 Stroller And Car Seat 2 in 1 prams uk (Elana)

  24. HaroldBig表示:

    semaglutide online: buy semaglutide online – rybelsus coupon

  25. RamonRek表示:

    buy ozempic pills online: ozempic generic – buy ozempic pills online

  26. High stake表示:

    Also visit my blog: High stake

  27. Buy bunk bed表示:

    How To Outsmart Your Boss On Bunk Bed Price Uk Buy bunk bed

  28. scam表示:

    Greetings! This is my first visit to your blog! We are a group of volunteers and starting a
    new project in a community in the same niche.
    Your blog provided us beneficial information to work on. You have done a marvellous job!

  29. HaroldBig表示:

    semaglutide online: semaglutide tablets – semaglutide tablets

發佈留言

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