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,498,454 Responses

  1. Demi表示:

    Forget L Shaped Bunk: 10 Reasons Why You Do Not Need It l shaped bunk beds with wardrobe (Demi)

  2. treadmills Uk表示:

    What’s The Job Market For Treadmills UK Reviews
    Professionals? treadmills Uk

  3. JesusLealt表示:

    sweet bonanza: sweet bonanza guncel – sweet bonanza slot

  4. The Most Worst Nightmare Concerning Electric Stoves Fires Be Realized
    Electric Stove Fires

  5. See What Replace Bifold Doors Tricks The Celebs Are Utilizing
    replace bifold doors; http://smi-webdemo-foodus.Kro.Kr/,

  6. Tips For Explaining Single Stroller To Your Mom single stroller near Me

  7. The 10 Most Terrifying Things About Best Car Seat Newborn Car seat newborn

  8. Darrelherse表示:

    bonus veren slot siteleri: slot siteleri guvenilir – slot kumar siteleri

  9. evisa vietnam表示:

    Its such as you read my thoughts! You seem to know so much about this, like you wrote the ebook in it or something.

    I believe that you just can do with some % to drive the message home a bit, but other than that, this is magnificent blog.
    An excellent read. I will definitely be back.

  10. Darrelherse表示:

    oyun siteleri slot: en iyi slot siteleri – en iyi slot siteler

  11. JesusLealt表示:

    deneme bonusu veren siteler: en iyi slot siteleri 2024 – 2024 en iyi slot siteleri

  12. JesusLealt表示:

    sweet bonanza mostbet: sweet bonanza siteleri – sweet bonanza nas?l oynan?r

  13. Vivian表示:

    Need Inspiration? Try Looking Up Situs Togel Dan Slot Terpercaya toto online
    terbaik – Vivian

  14. Truly no matter if someone doesn’t know after that its up to other people that they will assist, so here it takes place.

  15. My partner and I stumbled over here from a different web address and thought I might as well check things out.
    I like what I see so i am just following you. Look
    forward to exploring your web page yet again.

  16. Loyd表示:

    The Secret Secrets Of Coffee Maker Bean To Cup home
    bean to cup coffee machine – Loyd

  17. Very nice article. I absolutely appreciate this website. Stick with it!

  18. Guy表示:

    7 Simple Strategies To Totally Doing The Mental Health Assessments mental health status assessment – Guy

  19. socialwork.pk表示:

    Good post. I learn something new and challenging on sites I stumbleupon everyday.
    It will always be helpful to read through content from other writers and use something from other sites.

  20. Darrelherse表示:

    slot siteleri: 2024 en iyi slot siteleri – deneme veren slot siteleri

  21. Darrelherse表示:

    yeni slot siteleri: slot siteleri guvenilir – slot bahis siteleri

  22. Trouvez des prestataires qualifiés pour vos travaux de construction et de rénovation grâce à Travaux Online.
    Des artisans de confiance et une assistance complète tout
    au long du projet.

  23. For newest news you have to pay a quick visit world-wide-web and on web I found this web site as
    a best site for most up-to-date updates.

  24. Wow, that’s what I was looking for, what a stuff!

    present here at this weblog, thanks admin of this web site.

  25. Активные игроки в онлайн покер получают шансы определить главное событие 2021 года – https://kzpokerdomonline.com/ МЕ spf autumn!

  26. Darrelherse表示:

    deneme bonusu veren siteler: guvenilir slot siteleri – en iyi slot siteleri 2024

  27. I’m gone to say to my little brother, that he should also
    visit this webpage on regular basis to take updated from most up-to-date news.

發佈留言

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