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,431,155 Responses

  1. RogerFaf表示:

    https://maxpillsformen.com/# Buy Tadalafil 5mg

  2. Medicines information for patients. What side effects can this medication cause?
    where can i get dilantin prices
    All trends of medication. Read information here.

  3. Pills information leaflet. What side effects can this medication cause?
    how to get generic levitra no prescription
    Everything information about medicament. Get here.

  4. вызов нарколога на дом частная скорая помощь [url=www.mediaworld.ukrbb.net/viewtopic.php?f=49&t=5477]вызов нарколога на дом частная скорая помощь[/url] .

  5. Have you ever thought about writing an ebook or guest authoring on other blogs?
    I have a blog based on the same topics you discuss and would love to have you share some stories/information. I know my readers would value your work.

    If you are even remotely interested, feel free to send me an email.

  6. Great suggestions on pet dog grooming! I constantly have problem with brushing my pet dog’s thick layer portable dog groomers

  7. RogerFaf表示:

    http://maxpillsformen.com/# buy cialis pill

  8. Barry Bowen表示:

    Exploring innovative designs present day enhances aesthetic appeal properties owned thoroughly enjoyed greatly overall positive impact made greatly appreciated locally recognized endlessly inspiring others seek same goals achieved successfully too ! roofers near me

  9. Joshua Ruiz表示:

    I lately attended a jiu-jitsu workshop that opened my eyes to brand-new methods! For those that want to explore more regarding advanced relocations, have a look at jiu jitsu near me for some superb insights

  10. What’s up, the whole thing is going well here and ofcourse every one is sharing information, that’s actually excellent,
    keep up writing.

  11. наркологическая скорая в москве [url=https://taksafonchik.borda.ru/?1-2-0-00001312-000-0-0-1730876668]наркологическая скорая в москве[/url] .

  12. Alpha Spectre表示:

    Howdy! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche.

    Your blog provided us useful information to
    work on. You have done a wonderful job!

  13. viagra表示:

    When someone writes an article he/she maintains the plan of a user in his/her brain that how a user can understand it.
    Therefore that’s why this post is amazing. Thanks!

  14. copd exacerbation prevention support services copd exacerbation prevention education copd exacerbation prevention symptom management applications
    medical monitoring for copd copd symptoms copd exacerbation prevention support group facilitation
    copd education
    copd exacerbation severity scale copd exacerbation prevention advocacy campaigns fatigue in copd
    copd rehabilitation programs chronic respiratory failure in copd copd exacerbation severity scale

  15. вызов нарколога на дом частная скорая помощь вызов нарколога на дом частная скорая помощь .

  16. RogerFaf表示:

    https://maxpillsformen.com/# п»їcialis generic

  17. RogerFaf表示:

    http://fastpillsformen.com/# Order Viagra 50 mg online

  18. Barrybib表示:

    ed online treatment: cheap cialis – ed pills cheap

  19. Manuel Rose表示:

    This was very beneficial. For more, visit Injury Recovry Center

  20. Your discussion on pressure washing recommendations is so worthwhile! Thanks for sharing those data with us pressure washing san antonio tx

  21. Essie Harvey表示:

    Such diverse talents exist within this city’s construction industry—it truly showcases what being part of such vibrant communities means today still today too! driveway coat sealer

  22. I appreciate your advice on choosing colors for exterior painting roofers

  23. An outstanding share! I have just forwarded this onto a coworker who had been conducting a little research on this.
    And he in fact bought me breakfast because I stumbled upon it for him…

    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending the time to discuss this matter here on your web page.

  24. Chad Garza表示:

    Well done! Discover more at injury lawyers

發佈留言

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