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,234,039 Responses

  1. We are a group of volunteers and opening a
    new scheme in our community. Your web site offered us with valuable information to work on. You have done a formidable job and our entire community will
    be grateful to you.

  2. Karin表示:

    The People Closest To Upvc Door Repair Near Me Share Some Big Secrets upvc door repair cost; Karin,

  3. CarrollUnton表示:

    deneme bonusu veren siteler mycbet.com http://ultrabet-tr.online/# ultrabet guncel

  4. LarryCoell表示:

    ultrabet yeni giris 1125 ultrabet tr online ultrabet tr online

  5. RhodaReN表示:

    casino ohne einzahlung bietet eine spannende und bequeme Möglichkeit, Ihre Lieblingsspiele bequem von zu Hause aus zu genießen. Mit einer großen Auswahl an Optionen können Sie jederzeit und überall Casino-Klassiker wie Poker, Blackjack und Spielautomaten spielen. Online-Casinos bieten ein spannendes Spielerlebnis mit realistischer Grafik und Live-Dealer-Optionen. Tauchen Sie noch heute in die Welt der online casinos ein und erleben Sie die Aufregung und die Vorteile des virtuellen Spielens Ihrer Lieblingsspiele.

  6. singa8表示:

    Good way of describing, and pleasant article to take information concerning my presentation topic,
    which i am going to present in school.

  7. сервис накрутки подписчиков в телеграм сервис накрутки подписчиков в телеграм .

  8. Hey there! This is kind of off topic but I need some advice from an established blog.
    Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things
    out pretty fast. I’m thinking about creating my own but I’m not sure
    where to begin. Do you have any ideas or suggestions? With thanks

  9. Derekfar表示:

    Canl? Casino Siteleri: Casino Siteleri – Deneme Bonusu Veren Siteler

  10. Тут можно преобрести сейфы для оружия пистолетные оружейный сейф купить

  11. Derekfar表示:

    deneme bonusu veren siteler mycbet.com: deneme bonusu veren siteler betturkey – deneme bonusu veren siteler mycbet.com

  12. LarryCoell表示:

    matadorbet matadorbet giris matadorbet bid

  13. Тут можно преобрести оружейный сейф на заказ москва купить оружейный сейф для пистолета

  14. Тут можно преобрести купить оружейный шкаф купить сейф для охотничьего карабина

  15. No matter if some one searches for his essential thing,
    thus he/she desires to be available that in detail,
    thus that thing is maintained over here.

  16. Тут можно преобрести оружейные шкафы и сейфы сейф оружейный цена

  17. Derekfar表示:

    deneme bonusu veren siteler denemebonusu2026.com: deneme bonusu veren siteler yeni – deneme bonusu veren siteler yeni

  18. Greetings! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?

    My site looks weird when browsing from my apple iphone.
    I’m trying to find a template or plugin that might be able to fix this
    issue. If you have any suggestions, please share. Thank you!

  19. HassanbuifS表示:

    https://slot-tr.online/# slot tr online
    deneme bonusu veren siteler

  20. 10 Meetups On Coffee Machines Sage You Should Attend coffee Machines Uk

  21. 5 Laws That’ll Help The Double Glazing Condensation Repair Kit Industry double glazed repairs

  22. сервис для накрутки подписчиков в инстаграме сервис для накрутки подписчиков в инстаграме .

  23. Thermage表示:

    Way cool! Some extremely valid points! I appreciate you writing this post
    and also the rest of the website is really good.

  24. LarryCoell表示:

    slot oyunlar? slot oyunlar? slot oyunlar?

  25. double表示:

    How To Become A Prosperous Glassrepair Even If You’re Not Business-Savvy double

  26. What You Should Be Focusing On Making Improvements To Pragmatic Site 프라그마틱 슬롯버프

發佈留言

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