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,228,259 Responses

  1. Link exchange is nothing else but it is just placing the
    other person’s web site link on your page at appropriate place and other person will also do
    same for you.

  2. Davidtes表示:

    cipro for sale ciprofloxacin 500mg buy online where can i buy cipro online

  3. Ronaldpsymn表示:

    https://ciprofloxacin.tech/# cipro 500mg best prices

  4. I’m not that much of a online reader to be honest but your sites really nice, keep
    it up! I’ll go ahead and bookmark your site to come back in the future.

    Many thanks

    my site … Poway Blonde Specialist

  5. I am truly pleased to read this blog posts which consists of lots of useful data, thanks for providing these data.

  6. Для каждого слота начисляют по a hundred фриспинов, то есть суммарно игрок может получить one thousand бесплатных вращений.

  7. RandallTaulp表示:

    tamoxifen and osteoporosis: tamoxifen pill – femara vs tamoxifen

  8. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each
    time a comment is added I get three e-mails with the same comment.
    Is there any way you can remove me from that service?
    Appreciate it!

  9. CUROZ表示:

    I read this post completely concerning the difference of most up-to-date and preceding
    technologies, it’s awesome article.

  10. asbestos case表示:

    An Easy-To-Follow Guide To Mesothelioma Lawyer asbestos case

  11. Everyone loves what you guys are up too. This kind of clever work and exposure!
    Keep up the terrific works guys I’ve added you guys to my own blogroll.

    Feel free to visit my homepage: 해외선물 대여업체

  12. WilliamRhype表示:

    http://nolvadex.life/# nolvadex 10mg

  13. Matthewevarm表示:

    win — букмекерская контора 1вин. Нынешняя вариация сайтов для ставок и игры на реальные деньги поражает. Для российских игроков открыты десятки ресурсов 1win

  14. MLB중계表示:

    It’s truly a great and helpful piece of info. I am happy that you just shared this helpful info with us.
    Please stay us informed like this. Thank you for sharing.

    Take a look at my blog MLB중계

  15. Davidtes表示:

    cipro pharmacy buy cipro ciprofloxacin 500 mg tablet price

  16. RandallTaulp表示:

    lisinopril 20 mg daily: lisinopril 10 mg online no prescription – where to buy lisinopril

  17. Ronaldpsymn表示:

    http://finasteride.store/# order generic propecia price

  18. RandallTaulp表示:

    purchase cipro: buy ciprofloxacin over the counter – ciprofloxacin generic price

  19. Davidtes表示:

    buy cytotec pills Cytotec 200mcg price buy cytotec

  20. Donaldvew表示:

    I stumbled upon your record while browsing as a service to interrelated dirt on the internet. It’s a dignity post! Please continue to share out and heed the message updated. https://sites.google.com/view/scaryhorrorgames/

  21. Davidtes表示:

    order lisinopril online us lisinopril pill zestril 20 mg tab

  22. RandallTaulp表示:

    purchase lisinopril: lisinopril 10 mg online no prescription – lisinopril price uk

  23. discover this表示:

    Hello, i think that i saw you visited my site thus i came to “return the favor”.I am attempting to find things to enhance my website!I
    suppose its ok to use some of your ideas!!

  24. RandallTaulp表示:

    buy cytotec online: cytotec abortion pill – Cytotec 200mcg price

  25. Ronaldpsymn表示:

    http://cytotec.club/# cytotec buy online usa

  26. I read this piece of writing fully on the topic of the comparison of most recent and preceding technologies, it’s remarkable article.

  27. Appreciate this post. Will try it out.

  28. Davidtes表示:

    cost of generic propecia online buying cheap propecia without a prescription buy propecia online

發佈留言

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