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,238,082 Responses

  1. Emily表示:

    I know this web page provides quality depending articles or reviews and other information, is there any other website which provides these information in quality?

  2. Darrelherse表示:

    casino slot siteleri: yasal slot siteleri – en iyi slot siteler

  3. KevinSounc表示:

    http://denemebonusuverensiteler.win/# deneme bonusu veren siteler

  4. Darrelherse表示:

    en iyi slot siteleri: slot siteleri – deneme veren slot siteleri

  5. AntonioDiz表示:

    בבאר שבע הן סביבה שבה תוכל למצוא את הנערות היפות ביותר בארץ. נערות סקסיות וחרמניות שיודעות כיצד להתנהג עם גבר, וכיצד לגרום לו לשעות אחר הצהריים באמצע השבוע – אתה תמיד יכול למצוא נערות שירצו לבלות איתך בנעימים. לא צריך סיבות מיוחדות להגיע לבלות כאן. נערת ליווי

  6. 20 Tips To Help You Be More Successful At Mesothelioma Attorneys mesothelioma legal, tsdl11.godohosting.com,

  7. Your Family Will Be Grateful For Having This Double Glazed
    Windows Near Me Upvc Door Repairs

  8. Mesothelioma Lawyer Explained In Less Than 140 Characters mesothelioma Lawyers

  9. Darrelherse表示:

    bonus veren casino slot siteleri: guvenilir slot siteleri – en guvenilir slot siteleri

  10. Appreciating the persistence you put into your blog and detailed information you offer.

    It’s awesome to come across a blog every once in a while that isn’t the same old rehashed material.
    Wonderful read! I’ve saved your site and I’m adding your RSS feeds
    to my Google account.

  11. Howdy very nice blog!! Guy .. Beautiful .. Wonderful ..
    I’ll bookmark your website and take the feeds also?
    I’m glad to search out a lot of helpful info here in the put up,
    we’d like develop extra techniques on this regard, thank you for
    sharing. . . . . .

  12. Your Family Will Thank You For Getting This Auto Accident Lawsuits auto injury attorney near me

  13. Why You Should Focus On The Improvement Of Window Glass Repairs repair door (ns-solution.co.kr)

  14. Rio Hotel Las Vegas Offers Guests A Very Lively And Exciting Atmosphere 오피사이트

  15. I really like your blog.. very nice colors & theme. Did you create this website yourself or did
    you hire someone to do it for you? Plz answer back as I’m looking to
    create my own blog and would like to find out where u got this from.
    cheers

  16. Thank you for the good writeup. It actually was a enjoyment account it.
    Glance advanced to more delivered agreeable from you! However, how could we keep in touch?

  17. auto表示:

    The Reason Why You’re Not Succeeding At Jaguar Replacement Key auto

  18. Yes! Finally something about bokep 18+ nonton video bokep terbaru.

  19. Feel free to surf to my blog – Daycare Near Me

  20. You reported this exceptionally well.

  21. Alejandro表示:

    bookmarked!!, I really like your web site!

  22. pipewiki.org表示:

    I’m not that much of a internet reader to be
    honest but your sites really nice, keep it up!

    I’ll go ahead and bookmark your website
    to come back later on. Many thanks https://pipewiki.org/wiki/index.php/User:Jack189914256

  23. Incredible Blog Write-up
    Incredible, what an perceptive and reflective work !
    I stumbled upon myself nodding along as I scanned through your scrutiny of
    this vital matter.
    Your contentions were meticulously studied and
    conveyed in a lucid, compelling manner.
    I particularly admired how you were able to refine the fundamental subtleties and
    subtleties at operation , without oversimplifying
    or overlooking the obstacles .
    This write-up has presented me a lot to contemplate .
    You’ve certainly expanded my awareness and shifted my
    outlook in certain significant manners .
    Appreciation for taking the time to share your proficiency on this topic .
    Entries like this are extremely a invaluable addition to the
    conversation. I eagerly await experiencing what other perceptive content you have in
    reserve .

    Here is my blog: ebet com register login

  24. See What Glazing Repairs London Tricks The Celebs Are Utilizing Glazing Repairs London

  25. Hi would you mind letting me know which hosting company you’re utilizing?
    I’ve loaded your blog in 3 completely different browsers and I must say this
    blog loads a lot quicker then most. Can you recommend a good internet hosting provider at
    a reasonable price? Thanks, I appreciate it!

  26. Diplomi_kiEa表示:

    Добрый день!
    Купить документ о получении высшего образования можно у нас.
    sergei-cheremushkin.blogspot.com/2010/03/blog-post

  27. Jeannine表示:

    Useful info. Lucky me I found your web site by accident, and I am surprised why this coincidence didn’t took
    place earlier! I bookmarked it.

發佈留言

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