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,230,904 Responses

  1. I’m excited to find this website. I want to to thank you
    for ones time for this particularly fantastic read!!
    I definitely really liked every part of it and I have you
    saved to fav to see new stuff on your blog.

  2. The Reasons Why Suzuki Celerio Replacement Key Will Be Everyone’s Desire
    In 2023 Suzuki Car Keys Replacement

  3. Easydrorbix表示:

    rx discount pharmacy: toronto pharmacy viagra – kroger pharmacy gabapentin

  4. Caitlyn表示:

    An Intermediate Guide To Sectional L Shaped Sofa l shaped sectional leather – Caitlyn,

  5. Easydrorbix表示:

    doc morris pharmacy artane: real pharmacy rx generic viagra – ED Trial Pack

  6. tấm compact表示:

    Good post. I absolutely appreciate this website. Thanks!

  7. Hello! Someone in my Myspace group shared this website
    with us so I came to take a look. I’m definitely enjoying
    the information. I’m bookmarking and will be tweeting this
    to my followers! Exceptional blog and excellent design and style.

  8. Easydrorbix表示:

    online medicine tablets shopping: Viagra Soft Flavored – cialis india pharmacy

  9. 10 No-Fuss Methods For Figuring Out Your Faux Leather Couch real leather couch (http://www.eden1004.kr/)

  10. I want to to thank you for this good read!! I certainly enjoyed every bit of it.
    I have you book-marked to check out new things you post…

  11. The 10 Most Scariest Things About Adult.ADHD Test Female Adhd Test Uk

  12. Blog No表示:

    It’s genuinely very complicated in this busy life to listen news on TV, therefore I only use internet for that purpose, and get the most recent news.

  13. Easydrorbix表示:

    rx software pharmacy: reliable online pharmacy accutane – community rx pharmacy

  14. Easydrorbix表示:

    pharmacy cost of cialis: pioneer rx pharmacy software – legal online pharmacy review

  15. Amazing things here. I am vеry happy to see your post.
    Thanks sо uch and I аm looking abead to contact you.
    Will you kindly drop me a e-mail?

  16. Robin表示:

    Wow, that’s what I was seeking for, what a stuff!
    existing here at this website, thanks admin of this web site.

  17. kraken hydra表示:

    I love your blog.. very nice colors & theme. Did you create this website yourself or
    did you hire someone to do it for you? Plz reply as I’m
    looking to create my own blog and would
    like to know where u got this from. thank you

  18. Latosha表示:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You clearly know what youre talking about, why waste your intelligence on just posting
    videos to your blog when you could be giving us something enlightening to read?

  19. Kevinamith表示:

    Уборка помещений пожара http://uborka-ot-pozhara.ru/

  20. Drstobeese表示:

    cialis online pharmacy usa: online pharmacy store in delhi – can i buy viagra in pharmacy

  21. Eltonwag表示:

    женский ретрит https://ретриты.рф

  22. Whoa! This blog looks exactly like my old one! It’s on a completely different subject but it has pretty much
    the same page layout and design. Excellent choice of
    colors!

  23. Guide To Cost Of Private ADHD Assessment UK: The Intermediate Guide On Cost Of Private ADHD Assessment
    UK Private Adhd Assessment Uk

  24. Esteban表示:

    WOW just what I was looking for. Came here by searching for Blog

  25. Sarah Turner表示:

    Excellent website you have here but I was curious if you knew of any forums that
    cover the same topics talked about here? I’d really like to be a part of
    online community where I can get feedback from other experienced individuals that share
    the same interest. If you have any suggestions, please let me know.
    Thank you!

  26. Drstobeese表示:

    restore rx pharmacy: Tofranil – rite aid pharmacy viagra cost

  27. Wonderful blog! I found it while browsing on Yahoo News.

    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Many thanks

  28. Easydrorbix表示:

    xalatan online pharmacy: good rx pharmacy – australia online pharmacy viagra

發佈留言

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