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,539,666 Responses

  1. Lilly Roberts表示:

    I love how you emphasized regular maintenance for asphalt surfaces! Keeping them in shape is so important. Discover more at asphalt company jacksonville fl

  2. Luis Owen表示:

    I used to think maid services were just for the wealthy, but they’re so affordable! Learn more at house cleaners

  3. Sushi swap表示:

    hello!,I love your writing so a lot! percentage we
    keep up a correspondence more approximately your post on AOL?
    I need a specialist on this space to unravel my problem.
    May be that is you! Looking forward to see you.

  4. Service was fantastic and expense was likewise
    great.

  5. I will immediately snatch your rss as I can’t to
    find your e-mail subscription link or e-newsletter service.
    Do you’ve any? Please allow me recognize in order that I could subscribe.
    Thanks.

  6. BradleyEdino表示:

    sweet bonanza slot sweet bonanza sweet bonanza oyna

  7. 진주에 급하게 돈 찾는 분 있으신가요? 진주 노래방알바에 한 번 방문 해주세요

  8. Great blog post! If you’re dealing with HVAC issues, check out my go-to site for repairs: https://speedy-wiki.win/index.php/HVAC_Troubleshooting_and_Repair_in_Topanga

  9. Thanks for the informative content. More at Roof Repair Mesa

  10. Pretty! This was an incredibly wonderful article. Thank you for
    supplying this information.

  11. Wallacefex表示:

    slot casino siteleri: en kazancl? slot oyunlar? – en cok kazand?ran slot oyunlar?

  12. If you are going for finest contents like me, simply pay a quick visit this
    web site all the time since it provides feature contents, thanks

  13. Fletcher表示:

    Double Glazed Window Installers Tools To Streamline Your Daily Lifethe One Double Glazed Window Installers Trick That Everybody Should Learn double glazed window installers (Fletcher)

  14. JasonUnsoG表示:

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

  15. GichardPheve表示:

    Thanks for another informative site. The place else may I am getting that type of info written in such a perfect way? I have a undertaking that I am simply now running on, and I have been on the look out for such info.
    https://millionigrushek.ru/

  16. Ashley表示:

    I’ve been surfing on-line more than 3 hours nowadays, yet I by
    no means discovered any interesting article like yours.
    It’s pretty price enough for me. In my view, if all webmasters and bloggers made excellent content as you probably did, the net might be much more helpful than ever before.

  17. Hey! I’m at work surfing around your blog from
    my new iphone 4! Just wanted to say I love reading your blog and look forward to all
    your posts! Carry on the excellent work!

  18. My brother recommended I might like this web site.
    He was totally right. This post truly made my day.
    You cann’t imagine just how much time I had spent for this information! Thanks!

  19. Lon表示:

    15 Gifts For The Back Injury Settlement Lover In Your Life injurys attorney near me (Lon)

  20. JasonUnsoG表示:

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

  21. WOW just what I was looking for. Came here by searching for nagano fat burning tonic
    reviews

    Stop by my web page does nagano tonic work

  22. A片表示:

    I’m not sure why but this weblog is loading very slow for me.
    Is anyone else having this problem or is it a problem on my end?

    I’ll check back later on and see if the problem
    still exists.

    Feel free to visit my blog post: A片

  23. Greetings, I do believe your site could be having
    web browser compatibility issues. Whenever I take a look at your website in Safari,
    it looks fine however, if opening in I.E., it has some overlapping issues.

    I just wanted to give you a quick heads up! Other than that,
    fantastic site!

  24. FrankEmine表示:

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

  25. 전북호빠表示:

    전북 여성들의 놀이터 전북호빠가 있습니다.

  26. JasonUnsoG表示:

    https://slotsiteleri25.com/# slot casino siteleri

  27. BradleyEdino表示:

    slot oyunlar? puf noktalar? slot casino siteleri en cok kazand?ran slot oyunlar?

  28. Vi skriver också om spelsajternas bonusar såatt du kan hitta en bonus som passar dig.

發佈留言

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