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,467,462 Responses

  1. Ada表示:

    What’s Holding Back The Buy Our C Driving License Gothenburg
    Industry? Köpa C Körkort (Ada)

  2. FrankEmine表示:

    denemebonusuverensiteler25: deneme bonusu veren siteler yeni – yeni deneme bonusu veren siteler

  3. BradleyEdino表示:

    canl? casino siteleri canl? casino siteleri guvenilir casino siteleri

  4. поставить капельницу от запоя на дому цена поставить капельницу от запоя на дому цена .

  5. вывод из запоя ростовская область вывод из запоя ростовская область .

  6. 부산호빠表示:

    제의2 수도 부산!! 왜 제 2의 수도인지 저희 부산호빠가 제대로 보여드립니다.

  7. セックス ロボットrewriting history,and the list goes on.

  8. I like the helpful info you provide on your articles. I will bookmark your weblog and take a look at once more here frequently.

    I am moderately sure I will learn a lot of new stuff right here!
    Best of luck for the following!

  9. Hulda Lopez表示:

    “The staff extremely took their time to help me find the suitable fit at flooring in winnipeg

  10. вывод из запоя цены ростов на дону вывод из запоя цены ростов на дону .

  11. BradleyEdino表示:

    denemebonusuverensiteler25 deneme bonusu veren siteler deneme bonusu veren siteler yeni

  12. I do consider all the concepts you’ve introduced to your post.
    They are very convincing and will definitely work.
    Nonetheless, the posts are very brief for starters.
    May you please prolong them a little from subsequent time?
    Thanks for the post.

    My blog … floor protection

  13. Amazing! Its genuinely awesome paragraph, I have got much clear idea concerning from this piece of writing.

  14. Nice post. I was checking constantly this blog and I am impressed!
    Very useful info particularly the last part 🙂 I care for such info much.

    I was seeking this particular info for a long time. Thank you and good luck.

  15. Howdy, I do think your website could be having internet browser compatibility problems.
    Whenever I take a look at your site in Safari, it looks fine however when opening
    in Internet Explorer, it’s got some overlapping issues.

    I merely wanted to give you a quick heads up! Other than that, excellent site!

    My website: chapel of the flowers

  16. Alvin Sanchez表示:

    I value these insights into dealing with possible pipes crises proactively– especially stressing partnership between property owners 24/7 emergency plumber in Phoenix

  17. Wallacefex表示:

    en kazancl? slot oyunlar?: slot siteleri – slot siteleri

  18. Modesto表示:

    Routine monitoring and follow-ups are likewise important for safety and efficiency.

  19. Troy Miles表示:

    I took my cell into ### anykeyword ### the day gone by after dropping it Phone repair Boondall

  20. Dermal fillers are another option for wrinkle decrease, claimed Nikhil Dhingra, MD, of
    Spring Street Dermatology.

  21. Jeremy Ingram表示:

    The before-and-after photos of your deck project were stunning! It’s inspiring to see such results. Find more transformations at sunroom installation near me

  22. Semaglutide and tirzepatide are type 2 diabetic issues drugs
    used off-label for weight management.

  23. Activating reduced power setting lessens power usage by limiting some phone functions.

  24. Spot on with this write-up, I honestly believe this website needs much more attention. I’ll probably be
    returning to see more, thanks for the advice!

  25. DavidTouts表示:

    http://denemebonusuverensiteler25.com/# deneme bonusu veren siteler

  26. DavidTouts表示:

    https://casinositeleri25.com/# Casino Siteleri

  27. JasonUnsoG表示:

    http://sweetbonanza25.com/# sweet bonanza oyna

發佈留言

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