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,228,230 Responses

  1. Roseanne表示:

    The 10 Most Terrifying Things About 2 In 1 Travel System baby pram 2 in 1 (Roseanne)

  2. Door And Window Doctor Tools To Improve
    Your Everyday Lifethe Only Door And Window Doctor Trick That Every
    Person Must Be Able To Door And Window Doctor

  3. This is really interesting, You’re a very skilled blogger.

    I have joined your feed and look forward to seeking more of your
    magnificent post. Also, I have shared your site in my social networks!

  4. ClydePab表示:

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

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

  6. Are You Getting Tired Of Train Derailment Lawyer? 10 Inspirational Sources
    That Will Revive Your Passion Serious truck crash Attorney

  7. Derekfar表示:

    ultrabet bonus: ultrabet – ultrabet giris

  8. Its like you read my mind! You appear to grasp so much approximately this, such as you wrote the book in it or something.
    I feel that you just could do with a few percent to
    power the message home a bit, however other than that, that is magnificent blog.
    A fantastic read. I’ll certainly be back.

  9. Great site. Lots of helpful information here. I am sending it to several friends ans additionally sharing in delicious.
    And of course, thank you for your sweat!

  10. bookmarked!!, I really like your blog!

  11. When someone writes an paragraph he/she maintains the plan of a user in his/her mind that how a user can understand it.

    Therefore that’s why this article is perfect. Thanks!

  12. Inspiring quest there. What happened after? Good luck!

  13. Are You In Search Of Inspiration? Look Up Diagnosing ADHD
    In Adults How To Get An Adhd Diagnosis Uk Adults (https://Chessdatabase.Science)

  14. Its not my first time to pay a visit this website, i am visiting this web site dailly
    and take good information from here daily.

  15. LarryCoell表示:

    deneme bonusu veren siteler denemebonusu2026.com deneme bonusu veren siteler betturkey deneme bonusu veren siteler denemebonusu2026.com

  16. LarryCoell表示:

    guvenilir casino siteleri guvenilir casino siteleri Canl? Casino Siteleri

  17. Diplomi_ckkn表示:

    купить проведенный диплом купить проведенный диплом .

  18. Diplomi_lxkn表示:

    купить диплом слесаря ремонтника 1russa-diploms.ru .

  19. Herototo表示:

    good for content , i give your apresiasion five star for you, good bro

  20. Meds information sheet. Generic Name.
    pantoprazole drug info
    Actual information about pills. Get here.

  21. It’s going to be end of mine day, however before end I am
    reading this enormous post to improve my experience.

  22. Diplomi_prkn表示:

    диплом спорт купить 1russa-diploms.ru .

  23. LarryCoell表示:

    guvenilir casino siteleri Canl? Casino Siteleri Casino Siteleri

  24. Diplomi_ksMl表示:

    купить диплом о высшем образовании в томске many-diplom77.ru .

  25. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material stylish.
    nonetheless, you command get got an shakiness over that you wish be delivering the following.
    unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike.

  26. CarrollUnton表示:

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

  27. Derekfar表示:

    guvenilir casino siteleri: casino siteleri win – Canl? Casino Siteleri

  28. Live Draw SGP表示:

    Saved as a favorite, I really like your web site!

    https://w1.datakeluaransgp.cfd/

發佈留言

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