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,519,496 Responses

  1. 12 Facts About Mesothelioma Compensation To Refresh Your Eyes
    At The Cooler. Cooler Mesothelioma Lawsuits

  2. The Three Greatest Moments In Built-In Fridge History best Fridge uk

  3. click here表示:

    With havin so much written content do you ever
    run into any issues of plagorism or copyright infringement?
    My site has a lot of exclusive content I’ve either created myself or outsourced but it appears a lot
    of it is popping it up all over the web without my permission. Do you know any techniques to help reduce content from being stolen? I’d really appreciate it.

  4. You’ll Be Unable To Guess Adult Adhd Assessment Uk’s Benefits adult Adhd assessment uk

  5. Darrelherse表示:

    guvenilir slot siteleri 2024: 2024 en iyi slot siteleri – canl? slot siteleri

  6. Lauren表示:

    This is the perfect blog for anyone who wants to find
    out about this topic. You realize a whole lot its almost hard to argue with you (not that I actually would want to…HaHa).
    You definitely put a new spin on a topic that has been discussed for many
    years. Wonderful stuff, just wonderful! https://socialmediastore.net/story18285829/denta-especialidades

  7. Gordon表示:

    Small Sectional With Pull Out Bed Tools To Ease Your Daily
    Lifethe One Small Sectional With Pull Out Bed Trick That Everyone Should Be Able To l sectional with pull out bed (Gordon)

  8. Hello colleagues, its impressive post about cultureand entirely explained, keep it up all the
    time.

    My web page … spring loaded slam latch

  9. Bariatric Mobility Scooters Techniques To Simplify Your Daily Lifethe One Bariatric Mobility Scooters Trick That Should Be
    Used By Everyone Be Able To bariatric mobility scooters

  10. gay porn表示:

    porn cannibalism

  11. Excellent article! We are linking to this great content on our site.
    Keep up the good writing.

  12. Outstanding post however I was wanting to know if you could
    write a litte more on this subject? I’d be very grateful if you could elaborate a little bit more.
    Many thanks!

  13. You’ll Never Be Able To Figure Out This Infant Car Seat
    Uk’s Secrets Infant Car Seat Uk

  14. KevinSounc表示:

    https://slotsiteleri.bid/# guvenilir slot siteleri 2024

  15. The 10 Most Terrifying Things About Bandar Togel Terpercaya bandar togel Terpercaya (https://www.Mixcloud.Com/)

  16. 5 Laws That Can Benefit The Mental Health Assessment Near Me Industry mental health screening and assessment in juvenile justice (https://dream-weaver.co.kr/bbs/board.php?bo_table=free&Wr_Id=1091082)

  17. 5 Killer Quora Answers To Integral Fridge Freezer integral fridge freezer

  18. The Steve Jobs Of L Shaped Bunk Beds For Kids Meet
    Your Fellow L Shaped Bunk Beds For Kids Enthusiasts. Steve Jobs Of The L Shaped
    Bunk Beds For Kids Industry low l shaped bunk beds

  19. 10 Things That Your Family Taught You About Robotic Vacuum Self Emptying robotic vacuum self emptying

  20. врач на дом капельница от запоя врач на дом капельница от запоя .

  21. KevinSounc表示:

    http://sweetbonanza.network/# sweet bonanza demo oyna

  22. 20 Reasons To Believe L Shape Leather Sofa Will Never Be Forgotten l shaped sectional leather – car-friends.co.kr,

  23. Andrewrible表示:

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

  24. click here表示:

    A fascinating discussion is definitely worth comment.
    I believe that you ought to write more on this issue, it may not be a taboo matter but
    usually people don’t speak about these subjects. To the next!

    Kind regards!!

  25. Garland表示:

    Nine Things That Your Parent Teach You About
    Cheap Pull Out Couch cheap pull out couch (Garland)

  26. How To Determine If You’re In The Mood For Link Togel Link Togel Resmi

  27. 11 “Faux Pas” That Are Actually Acceptable
    To Do With Your ADHD Test Adult screening test for adhd in adults

  28. Five Killer Quora Answers On Bifold Door Repairs Bifold door repairs

  29. You’ll Never Be Able To Figure Out This Electric Fire Suite Oak’s Secrets
    Electric Fire Suite Oak

  30. Landon表示:

    10 Wrong Answers To Common Link Togel Questions Do You Know The Right Answers?
    bandar toto – Landon

發佈留言

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