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,229,259 Responses

  1. The One Squirtingdildos Trick Every Person Should Learn squirting cock toy

  2. Ten Cheap Electric Wheelchairs That Really Change Your Life cheap Electric wheelchairs

  3. Nikefut表示:

    darknet seiten tor market url blackweb official website

  4. HassanbuifS表示:

    https://slot-tr.online/# en kazancl? slot oyunlar?
    deneme bonusu veren siteler denemebonusu2026.com

  5. For latest news you have to pay a quick visit world wide web and on the web
    I found this web site as a best web site for most up-to-date updates.

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

  7. fleshlite表示:

    15 Gifts For The Anal Fleshlight Lover In Your Life fleshlite

  8. Maxwell表示:

    You could definitely see your expertise within the article you write.

    The world hopes for more passionate writers such as you who aren’t afraid to mention how they believe.
    All the time follow your heart.

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

  10. LarryCoell表示:

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

  11. I have painstakingly appreciated the musings provided in this
    insightful blog commentary . The composer
    has poetically articulated several critical propositions
    that resound with me deeply .

    As an enthusiastic backer of avant-garde corporate enterprises , I would yearn to extend an
    proposal to you to examine the phenomenal
    possibilities available at Pragmatic Play.

    This versatile firm is at the vanguard of pioneering advancements ,
    offering a lively and valuable ecosystem for enthusiasts who possess a passion for
    eminence and a drive to push the parameters of what
    is realizable.

    I beseech you to weigh this suggestion and investigate the cornucopia of options that await .

    Please be at liberty to reach out if you have any
    inquiries or would yearn to discuss in additional
    detail .

    Most cordial wishes ,

    my site online casino prepaid cards

  12. What Is Programing Keys’ History? History Of Programing Keys Transponder Key Programming

  13. The Top Upvc Repairs Near Me Gurus Are Doing 3 Things
    window repair near Me

  14. Nikefut表示:

    dark web websites dark web market list darknet market

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

  16. Hello! I simply want to give you a huge thumbs up for your great info you
    have here on this post. I’ll be returning to your site for more soon.

  17. ai-porn.rocks表示:

    You need to be a part of a contest for one of
    the highest quality sites on the internet. I most certainly will highly
    recommend this blog!

  18. 10 Mobile Apps That Are The Best For L Shaped Couch
    Small L Shaped Couch (https://Royalbookmarking.Com/)

  19. HassanbuifS表示:

    http://matadorbet.bid/# matadorbet bid
    deneme bonusu veren siteler yerliarama.org

  20. aiporn.work表示:

    Awesome issues here. I’m very happy to peer your post.
    Thank you so much and I’m having a look ahead to touch you.
    Will you kindly drop me a mail?

  21. ThePokies104表示:

    Thanks for the good writeup. It in fact was a leisure account
    it. Look complex to more introduced agreeable from you! However, how could we communicate?

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

  23. LarryCoell表示:

    deneme bonusu veren siteler yerliarama.org deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler 2024

  24. Derekfar表示:

    ultrabet guncel: ultrabet giris – ultrabet giris

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

  26. LarryCoell表示:

    guvenilir casino siteleri casino siteleri win Canl? Casino Siteleri

  27. Rtp MHTOGEL表示:

    Absolutely beautiful message.
    Login MHTOGEL

  28. Link MHTOGEL表示:

    This is so relevant to me.
    Login MHTOGEL

  29. Stephensoobe表示:

    Ни одно застолье невозможно представить себе без освежающих безалкогольных напитков. Сейчас вам не придется тратить свое время и силы на то, чтобы ездить в магазины и покупать соки, минеральную воду доставка алкоголя москва партнер в ночных покупках

發佈留言

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