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,235,736 Responses

  1. Derekfar表示:

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

  2. LarryCoell表示:

    deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler 2024 deneme bonusu veren siteler mycbet.com

  3. After looking over a number of the blog articles on your blog, I really appreciate
    your way of writing a blog. I saved it to my bookmark webpage list
    and will be checking back soon. Please visit my web site as
    well and let me know your opinion.
    https://w2.angkasetan.uk/

  4. LarryCoell表示:

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

  5. 7 Essential Tips For Making The Maximum Use Of Your Private ADHD Assessment Near Me Private Assessment For Adhd Cost

  6. LarryCoell表示:

    az parayla cok kazandiran slot oyunlar? az parayla cok kazandiran slot oyunlar? en kazancl? slot oyunlar?

  7. CarrollUnton表示:

    deneme bonusu veren siteler 2024 http://casinositeleri.win/# guvenilir casino siteleri

  8. Тут можно преобрести сейф оружейный купить в москве сейф оружейный

  9. The Biggest Problem With Assessing Adhd In Adults And How To Fix It
    adhd Assessment Scotland

  10. Heya! I just wanted to ask if you ever have any trouble with
    hackers? My last blog (wordpress) was hacked and I ended up
    losing a few months of hard work due to no back up. Do you
    have any methods to protect against hackers?

  11. I constantly spent my half an hour to read this blog’s articles or reviews every day along with a cup
    of coffee.

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

  13. Five Killer Quora Answers To Fabric Corner Sofa Beige Fabric corner sofa

  14. ClydePab表示:

    denemebonusuverensiteler.top: denemebonusuverensiteler.top – deneme bonusu veren siteler yerliarama.org

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

  16. Derekfar表示:

    guvenilir casino siteleri: Canl? Casino Siteleri – Deneme Bonusu Veren Siteler

  17. Antonetta表示:

    Where Will Virtual Mystery Boxes Be 1 Year From In The Near Future?
    online mystery box sites (Antonetta)

  18. This Is A Guide To Prams 2 In 1 In 2023 2in1 pushchair
    (linkagogo.trade)

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

  20. Oh my goodness! Amazing article dude! Thanks, However I am going through troubles with your RSS.
    I don’t know the reason why I am unable to join it.
    Is there anybody else getting the same RSS issues?
    Anyone who knows the solution will you kindly respond?
    Thanks!!

  21. LarryCoell表示:

    ultrabet bonus ultrabet ultrabet bonus

  22. CarrollUnton表示:

    deneme bonusu veren siteler yerliarama.org https://casinositeleri.win/# Casino Siteleri

  23. Latosha表示:

    Three Greatest Moments In Coffee Machine History best home coffee maker (Latosha)

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

  25. Jason表示:

    9 Lessons Your Parents Teach You About Double Glazing Repair Near Me double glazing repair near me
    Jason,

  26. 15 Best 3 Wheel Travel System Bloggers You
    Must Follow best 3 Wheel travel system

發佈留言

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