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,231,165 Responses

  1. See What Mesothelioma Litigation Tricks The Celebs Are Using Mesothelioma Litigation

  2. Davidtes表示:

    purchase cipro cipro ciprofloxacin buy cipro online canada

  3. Davidtes表示:

    tamoxifen hot flashes nolvadex only pct how does tamoxifen work

  4. Ronaldpsymn表示:

    http://finasteride.store/# propecia brand name

  5. Popular Slots表示:

    A Step-By-Step Instruction For Best Slot Payouts Popular Slots

  6. RandallTaulp表示:

    generic propecia: cost generic propecia pill – get propecia without rx

  7. WilliamRhype表示:

    https://nolvadex.life/# hysterectomy after breast cancer tamoxifen

  8. WilliamRhype表示:

    http://cytotec.club/# cytotec online

  9. When someone writes an piece of writing he/she
    keeps the plan of a user in his/her mind that how
    a user can know it. Therefore that’s why this article is perfect.
    Thanks!

  10. Is Citroen Replacement Key Cost As Important As Everyone Says?

    Citroen Xsara Key

  11. WilliamRhype表示:

    http://lisinopril.network/# lisinopril tabs

  12. 10 Things Everyone Hates About Bean To Cup Coffee Machines coffee machine beans to cup

  13. Upvc Windows Near Me Tools To Streamline Your Daily Lifethe One Upvc
    Windows Near Me Trick That Everyone Should Learn upvc windows near me

  14. latex-free表示:

    What Is Double Mattress Memory Foam? And How To Utilize It latex-free

  15. Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I
    may return once again since i have book marked it.
    Money and freedom is the greatest way to change, may you be rich and continue to guide other people.

  16. Five Killer Quora Answers On Double Glazed Near Me Double Glazed Near Me

  17. Have you ever considered writing an e-book or
    guest authoring on other blogs? I have a blog
    based upon on the same information you discuss and would really like to have you share some stories/information. I
    know my viewers would enjoy your work. If you’re even remotely interested,
    feel free to send Physiotherapie Near Me an e mail.

  18. RandallTaulp表示:

    cost of propecia online: cost of cheap propecia price – cost propecia without insurance

  19. Why You Should Concentrate On Improving ADHD Tests For Adults Adhd online testing

  20. play Slots表示:

    The History Of Winning Slots play Slots

  21. WilliamRhype表示:

    http://ciprofloxacin.tech/# buy generic ciprofloxacin

  22. Hi i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also make
    comment due to this sensible post.

    my web-site … classic free movies

  23. Awesome posts, Thanks.

  24. 4 Dirty Little Tips About Repair A Window Industry Repair A Window Industry
    Window Repairs

  25. Locksmiths Near Me Auto Is The Next Hot Thing In Locksmiths Near Me Auto Auto key smith near me

  26. Good posts With thanks!

  27. Hey I know this is off topic but I was wondering if you knew of any widgets
    I could add to my blog that automatically tweet
    my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe
    you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading
    your blog and I look forward to your new updates.

  28. See What Double Glazed Windows Near Me Tricks The Celebs Are Using double glazed windows Near me

  29. WilliamRhype表示:

    http://ciprofloxacin.tech/# п»їcipro generic

  30. Guide To Managing Adhd Without Medication: The Intermediate Guide Towards Managing Adhd Without Medication Adhd Without medication

發佈留言

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