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,466,947 Responses

  1. FrankEmine表示:

    guvenilir slot siteleri: slot oyunlar? – guvenilir slot siteleri

  2. BOKEP TERBARU表示:

    I do believe all the concepts you have offered in your post.

    They’re really convincing and will certainly work. Nonetheless, the posts are very
    brief for newbies. Could you please lengthen them a bit from subsequent time?
    Thank you for the post.

  3. If you’re in need of quick HVAC repair services in Los Angeles, I strongly recommend checking out https://wiki.natlife.ru/index.php/Emergency_HVAC_Repairs_in_Winnetka_CA

  4. Wallacefex表示:

    canl? casino siteleri: Casino Siteleri – en iyi kumar siteleri

  5. JasonUnsoG表示:

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

  6. Wallacefex表示:

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

  7. Libby表示:

    Easter Baskets – Make An Easter Date Supremely Sweet 오피사이트 (Libby)

  8. Thank you a lot for sharing this with all of us you really understand what you are talking about!
    Bookmarked. Kindly also visit my site =). We may have a link change contract between us

  9. 강남쪽 재미있게 놀면서 돈 벌고 싶으시다면 강남 노래방알바로 연락 줘보세요

  10. We stumbled over here coming from a different page and thought
    I should check things out. I like what I see so now i’m following you.

    Look forward to going over your web page for a second time.

    Also visit my homepage; Myron Golden Challenge

  11. JasonUnsoG表示:

    http://casinositeleri25.com/# en guvenilir casino siteleri

  12. JasonUnsoG表示:

    https://slotsiteleri25.com/# slot siteleri

  13. BradleyEdino表示:

    sweet bonanza oyna sweet bonanza demo oyna sweet bonanza yorumlar

  14. If you intend to make a grand entry, nothing beats stepping out of a limousine! Perfect for parties and red carpeting events. Obtain motivated by our ideas at limousine service near me

  15. sunwin表示:

    It’s amazing to pay a visit this web page and reading the views of all colleagues on the topic of this
    paragraph, while I am also zealous of getting familiarity.

  16. Alberto表示:

    It’s an awesome post in support of all the
    web viewers; they will obtain benefit from it I
    am sure.

  17. Ron表示:

    Nice post. I was checking continuously this blog and I’m
    impressed! Very useful information specially the last part 🙂 I care for such information a lot.
    I was seeking this particular info for a long time.
    Thank you and good luck.

  18. BradleyEdino表示:

    sweet bonanza slot sweet bonanza kazanma saatleri sweet bonanza demo oyna

  19. Jonathon表示:

    Hello friends, its great paragraph regarding cultureand fully explained, keep it up all the
    time.

  20. FrankEmine表示:

    az parayla cok kazandiran slot oyunlar?: az parayla cok kazandiran slot oyunlar? – guvenilir slot siteleri

  21. PeterAlact表示:

    Займы в Казахстане онлайн — быстрое оформление, минимальные требования, выгодные условия для получения нужной суммы микрозайм

  22. JasonUnsoG表示:

    http://casinositeleri25.com/# guvenilir casino siteleri

  23. 10 Myths Your Boss Has Concerning Best Price On LG Refrigerators Is lg Refrigerator good quality

  24. 10 Things That Your Family Teach You About Robot Vacuum Cleaner
    Reviews robot Vacuum cleaner reviews

  25. Nettie Carter表示:

    Just saw a neighbor get their roof power washed—I didn’t realize it could improve lifespan too! Power Washing

  26. I have read several just right stuff here. Certainly value bookmarking for revisiting.
    I surprise how so much attempt you place to create one
    of these excellent informative site.

  27. It’s remarkable in favor of me to have a web page, which is valuable for my
    knowledge. thanks admin

    Have a look at my webpage: donat89 daftar

  28. BradleyEdino表示:

    sweet bonanza guncel sweet bonanza sweet bonanza kazanma saatleri

  29. Today, while I was at work, my sister stole my apple ipad and tested
    to see if it can survive a forty foot drop, just so she can be a youtube
    sensation. My apple ipad is now destroyed and she has 83 views.
    I know this is entirely off topic but I had to share it with someone!

  30. JasonUnsoG表示:

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

發佈留言

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