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,229,372 Responses

  1. Derekfar表示:

    matadorbet giris: matadorbet – matadorbet giris

  2. HassanbuifS表示:

    https://matadorbet.bid/# matadorbet giris
    deneme bonusu veren yeni siteler

  3. Kristie表示:

    10 Quick Tips For Mesothelioma Legal Question mesothelioma law
    firm (Kristie)

  4. How Link Togel Has Changed My Life The Better link Togel Resmi

  5. Derekfar表示:

    Deneme Bonusu Veren Siteler: guvenilir casino siteleri – guvenilir casino siteleri

  6. ArnPaish表示:

    Are men who use ivermectin for goats and have multiple orgasms?|

  7. It’s nearly impossible to find experienced people for this topic, but
    you seem like you know what you’re talking about!
    Thanks

  8. There is certainly a lot to know about this subject.

    I like all of the points you have made.

  9. With havin so much content do you ever run into any issues of plagorism or copyright infringement?
    My site has a lot of unique content I’ve either authored myself or
    outsourced but it seems a lot of it is popping it
    up all over the internet without my authorization. Do you
    know any ways to help reduce content from being ripped off?

    I’d certainly appreciate it.

  10. Derekfar表示:

    matadorbet: matadorbet – matadorbet

  11. 5 Must-Know-Practices Of Porsche Key Replacement Cost For 2023 porsche cayenne replacement key Cost

  12. Thankfulness to my father who stated to me concerning this weblog, this website is truly amazing.

    https://w4.livedrawnevada.life/

  13. Hello there, just became aware of your blog through Google, and found that
    it is really informative. I am going to watch out for brussels.
    I will appreciate if you continue this in future.
    Lots of people will be benefited from your writing. Cheers!

    https://w6.hkmalamini.org/

  14. CarrollUnton表示:

    deneme bonusu veren siteler 2024 http://casinositeleri.win/# Casino Siteleri

  15. Mavis表示:

    Normally I don’t read post on blogs, however I wish to
    say that this write-up very compelled me to take a
    look at and do so! Your writing taste has been amazed me.
    Thank you, very great article.

  16. Result HK表示:

    I was suggested this blog via my cousin. I’m now not certain whether or not this post is written by means
    of him as no one else know such precise approximately my difficulty.
    You are incredible! Thank you!

    http://w1.mistiktogel.cfd/

  17. Тут можно преобрести сейф противопожарный сейф огнеупорный

  18. always i used to read smaller posts that as well clear their motive,
    and that is also happening with this piece of writing which I am reading at this place.

  19. Triangle Billiards & Bar Stools
    1471 Nisson Ꭱd, Tustin,
    ⲤA 92780, United Ꮪtates
    +17147715380
    Pool Table Repairs

  20. The 10 Scariest Things About Replacement
    Key For Fiat 500 Replacement Key For Fiat 500 (Morphomics.Science)

  21. ClydePab表示:

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

  22. penipu表示:

    Thanks for sharing your thoughts on penipu. Regards

  23. Marcy表示:

    Everything You Need To Learn About Automatic Folding
    Mobility Scooter Near Me autofold mobility scooter (Marcy)

  24. This blog was… how do I say it? Relevant!! Finally I have found something that helped me.
    Many thanks!

    https://w3.rumuscb.buzz/

  25. Everything is very open with a very clear explanation of the challenges.
    It was definitely informative. Your site is very helpful.
    Thanks for sharing!

    https://w5.livemacau.buzz/

  26. HassanbuifS表示:

    https://ultrabet-tr.online/# ultrabet guncel
    deneme bonusu veren siteler yeni

  27. I’d like to find out more? I’d like to find
    out more details.

  28. HassanbuifS表示:

    http://ultrabet-tr.online/# ultrabet giris
    deneme bonusu veren yeni siteler

發佈留言

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