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,366,349 Responses

  1. I’m curious to find out what blog system you happen to be utilizing?
    I’m having some minor security issues with my latest website and I’d like to find something more safe.
    Do you have any solutions?

    My web site … nerve fresh reviews

  2. Jeremypag表示:

    rybelsus pill rybelsus price buy semaglutide online

  3. uk88表示:

    What’s up, after reading this awesome piece of writing i am as well glad to share my familiarity
    here with colleagues.

  4. The No. Question Everybody Working In Lexus Key Replacement Near Me Needs To
    Know How To Answer Lexus car key; https://barbour-downs.mdwrite.net/,

  5. HaroldBig表示:

    Ozempic without insurance: buy cheap ozempic – buy ozempic

  6. Upvc Half Door Panels Techniques To Simplify Your Daily
    Lifethe One Upvc Half Door Panels Trick Every Individual Should Know How to remove a upvc Door panel

  7. RamonRek表示:

    buy ozempic pills online: Ozempic without insurance – ozempic

  8. Lashonda表示:

    The Reason Behind Volvo Key Replacement Is Everyone’s Passion In 2023 volvo
    truck key blank (Lashonda)

  9. bk8表示:

    It’s a shame you don’t have a donate button! I’d most certainly donate to this outstanding blog!
    I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will talk about this website with my Facebook group.
    Talk soon!

  10. HaroldBig表示:

    ozempic cost: ozempic online – ozempic coupon

  11. Wow, this article is nice, my sister is analyzing such
    things, thus I am going to inform her.

  12. Профессиональный сервисный центр по ремонту автомагнитол в Москве.
    Мы предлагаем: ремонт автомагнитолы
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  13. HaroldBig表示:

    buy rybelsus online: semaglutide cost – buy semaglutide online

  14. After implementing your Web 2.0 sites for backlinks strategy, it’s crucial to monitor the performance of your backlinks. Use tools like Google Analytics or Ahrefs to track referral traffic, and assess how your backlinks are influencing your site’s SEO. Regularly evaluating your strategy allows you to make adjustments and optimize your efforts.

  15. 10 Quick Tips About Pragmatic 라이브 카지노

  16. HaroldBig表示:

    buy semaglutide online: cheapest rybelsus pills – rybelsus price

  17. Frankevild表示:

    https://ozempic.art/# ozempic coupon

  18. 10 No-Fuss Methods For Figuring The Pragmatic Free Slot Buff You’re Looking For 프라그마틱

  19. Jeremypag表示:

    semaglutide online semaglutide cost semaglutide online

  20. Ruben表示:

    The 9 Things Your Parents Taught You About Bioethanol Fire ethanol
    fire; Ruben,

  21. 5 Killer Quora Answers On Situs Terpercaya Situs Terpercaya

  22. Really all kinds of fantastic material!

    Feel free to visit my website :: https://mostbet-bk.com

  23. Wow, superb blog format! How long have you ever been running a blog for?
    you make running a blog look easy. The full look of your website is magnificent, as smartly
    as the content material!

    My page; tonic greens customer reviews

  24. Diosa brought me to the CrashPad. I was drawn to the revolutionary act of queer/trans
    POC making queer porn for other queer and trans POC.

  25. slut表示:

    Excellent pieces. Keep writing such kind of information on your site.

    Im really impressed by your site.
    Hello there, You’ve performed an excellent job. I will definitely digg
    it and in my view recommend to my friends. I am confident
    they will be benefited from this site.

  26. Hello! This is kind of off topic but I need some
    help from an established blog. Is it hard to set
    up your own blog? I’m not very techincal but I can figure things out
    pretty quick. I’m thinking about setting up my own but I’m not
    sure where to start. Do you have any ideas or suggestions?

    Thanks

  27. HaroldBig表示:

    ozempic generic: ozempic cost – ozempic coupon

  28. web tasarım表示:

    web tasarım

    Wow, that’s what I was seeking for, what a information! present here at
    this website, thanks admin of this website.

  29. HaroldBig表示:

    buy ozempic pills online: ozempic cost – ozempic coupon

  30. Hi there, all is going nicely here and ofcourse every one is sharing information,
    that’s really excellent, keep up writing.

發佈留言

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