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,237,214 Responses

  1. sex webcams表示:

    Thank you for another informative website. Where else may just I get that type of info written in such a perfect method? I have a mission that I’m just now working on, and I have been at the look out for such info.

  2. LarryCoell表示:

    ultrabet giris ultrabet giris ultrabet bonus

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

  4. Someone essentially assist to make seriously articles I would state.
    This is the very first time I frequented your website page and
    to this point? I amazed with the analysis you made to make this
    actual put up extraordinary. Great process!

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

  6. Heya i’m for the first time here. I came across this board and I find It truly useful & it helped me out a
    lot. I hope to give something back and aid others like you helped
    me.

    My web-site – ezproxy.cityu.edu.hk

  7. LarryCoell表示:

    Deneme Bonusu Veren Siteler Casino Siteleri guvenilir casino siteleri

  8. spare表示:

    20 Amazing Quotes About Saab Key Cover spare

  9. 10 Tell-Tale Warning Signs You Should Know To Get A New Good Accident
    Lawyers good Accident lawyers Near Me

  10. The 3 Most Significant Disasters In Best Folding Treadmill For Small Space History Home Treadmills

  11. Derekfar表示:

    ultrabet giris: ultrabet yeni giris 1125 – ultrabet

  12. LarryCoell表示:

    slot tr online az parayla cok kazandiran slot oyunlar? slot oyunlar?

  13. rr88表示:

    It’s a shame you don’t have a donate button! I’d without a doubt donate to this brilliant blog!
    I suppose for now i’ll settle for bookmarking
    and adding your RSS feed to my Google account. I look forward to brand
    new updates and will share this website with my Facebook group.
    Talk soon!

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

  15. I read this article completely on the topic of the comparison of
    newest and earlier technologies, it’s awesome article.

  16. Wow, incredible blog layout! How long have you been blogging for?
    you make blogging look easy. The overall
    look of your web site is fantastic, as well as the content!

  17. What’s The Current Job Market For Retro Fridges Freezers Professionals Like?

    retro fridges Freezers

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

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

  20. Good article. I will be going through a few of these issues as well..

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

  22. ClydePab表示:

    ultrabet guncel: ultrabet tr online – ultrabet yeni giris 1125

  23. The Ultimate Cheat Sheet For Truck Attorney Near Me semi Truck attorney (championsleage.review)

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

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

  26. asbestos表示:

    People who have a history of asbestos exposure are at risk of developing mesothelioma.

  27. Thanks in favor of sharing such a fastidious thinking,
    paragraph is pleasant, thats why i have read it completely

  28. LarryCoell表示:

    Deneme Bonusu Veren Siteler Casino Siteleri casino siteleri win

  29. Derekfar表示:

    ultrabet giris: ultrabet bonus – ultrabet tr online

發佈留言

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