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,233,985 Responses

  1. The Little-Known Benefits Of Online Jobs Work From Home No Experience
    Work From Home And Earn Money

  2. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# buy cipro online canada

  3. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 40 mg without prescription

  4. The Most Negative Advice We’ve Ever Heard About How To Get Diagnosed With
    ADHD Private adult adhd Diagnosis

  5. RandallTaulp表示:

    ciprofloxacin generic: ciprofloxacin over the counter – ciprofloxacin order online

  6. Davidtes表示:

    buy cytotec pills buy cytotec Misoprostol 200 mg buy online

  7. quality表示:

    20 Important Questions To Ask About Desk Treadmills Before You Buy Desk
    Treadmills quality

  8. Hydra case表示:

    7 Tips To Make The Most Out Of Your What CSGO Cases To Invest In Hydra case

  9. Leslieprerm表示:

    наркологическая клиника https://krasnodar-narkolog.ru/

  10. 20 Private ADHD Websites That Are Taking The Internet By
    Storm private adhd assessment cheshire

  11. Davidtes表示:

    nolvadex only pct tamoxifen bone density clomid nolvadex

  12. Nice post. I was checking constantly this blog and
    I’m impressed! Extremely helpful information specifically the last part :
    ) I care for such information much. I was looking for this certain information for a very long time.
    Thank you and best of luck.

  13. I do not even understand how I stopped up right here,
    however I thought this submit was good. I don’t recognise who you might be however definitely you
    are going to a well-known blogger if you aren’t already.
    Cheers!

  14. RandallTaulp表示:

    tamoxifen depression: dcis tamoxifen – what happens when you stop taking tamoxifen

  15. Bernard表示:

    What You Need To Do With This Online Shopping Uk Amazon Fireglass Wind Guard Square [Bernard]

  16. The Benefits Of Shopping Online Uk At The Very Least Once In Your Lifetime Exterior Door Threshold Seal (https://Vimeo.com/931084156)

  17. RandallTaulp表示:

    Cytotec 200mcg price: Misoprostol 200 mg buy online – buy cytotec over the counter

  18. WilliamRhype表示:

    http://lisinopril.network/# lisinopril pills 2.5 mg

  19. telanjang表示:

    That is really interesting, You’re a very professional blogger.
    I have joined your rss feed and look ahead to looking for extra of your excellent post.
    Additionally, I have shared your site in my social networks

  20. 10 Life Lessons We Can Learn From Citroen C1
    Key Replacement citroen Relay key fob

  21. WilliamRhype表示:

    http://ciprofloxacin.tech/# cipro online no prescription in the usa

  22. This Is The Ultimate Guide To Designer Handbags Large Designer Handbags Pink

  23. 롤놀이터表示:

    Hey there outstanding website! Does running a blog such as this take a great deal of work?
    I have very little understanding of computer programming however I was hoping to
    start my own blog soon. Anyway, should you have any ideas or techniques for new blog owners please share.
    I understand this is off topic nevertheless I simply needed to ask.
    Thanks!

  24. RandallTaulp表示:

    tamoxifen and osteoporosis: nolvadex half life – aromatase inhibitor tamoxifen

  25. WilliamRhype表示:

    http://cytotec.club/# buy misoprostol over the counter

  26. fair Slots表示:

    Is Low Limit Slots The Greatest Thing There Ever Was?

    fair Slots

  27. 10 Tips For Quickly Getting Online Work work from home
    and earn money (http://vkeepw.evai.pl/home.Php?mod=space&uid=179006)

  28. WilliamRhype表示:

    http://nolvadex.life/# where to get nolvadex

  29. netvoyne.ru表示:

    14 Creative Ways To Spend Left-Over Double Glazed Repairs Near Me Budget double Glazed wood windows
    (netvoyne.ru)

發佈留言

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