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,371,451 Responses

  1. Mostbet表示:

    What’s up i am kavin, its my first time to commenting anyplace, when i read this post i thought i could also create comment due to this sensible post.

    Look into my web-site … Mostbet

  2. Wonderful blog! Do you have any helpful hints for aspiring writers?

    I’m planning to start my own site soon but I’m a
    little lost on everything. Would you recommend starting with a free platform
    like WordPress or go for a paid option? There are so many choices out there that I’m totally confused ..

    Any suggestions? Bless you!

    Also visit my blog … lottery defeater software app

  3. The 12 Best Key Cutting Aylesbury Accounts To Follow On Twitter double glazing repairs

  4. RamonRek表示:

    rybelsus coupon: semaglutide cost – semaglutide online

  5. Emory表示:

    Hi there! This is kind of off topic but I need some help from an established blog.
    Is it tough to set up your own blog? I’m not very techincal but I can figure things out pretty quick.

    I’m thinking about making my own but I’m not sure where to begin. Do you have any ideas or suggestions?

    With thanks

  6. I am regular reader, how are you everybody? This paragraph posted
    at this site is really pleasant.

  7. Fidel Cage表示:

    The Most Pervasive Issues With Adhd Assessment London adhd assessment uk online

  8. Jeremypag表示:

    ozempic cost ozempic Ozempic without insurance

  9. 9 Lessons Your Parents Taught You About Double Glazing Repair Near Me Double glazing repair near me

  10. BarryDof表示:

    http://rybelsus.shop/# buy semaglutide online

  11. Hi, I do believe this is a great website. I stumbledupon it ;
    ) I am going to 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 others.

  12. I am really enjoying the theme/design of your web
    site. Do you ever run into any web browser compatibility issues?
    A few of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Firefox.
    Do you have any tips to help fix this issue?

  13. Frankevild表示:

    http://rybelsus.shop/# rybelsus cost

  14. Jeremypag表示:

    rybelsus cost buy semaglutide online semaglutide tablets

  15. I love what you guys are usually up too. This kind of clever work and exposure!
    Keep up the terrific works guys I’ve added you guys to blogroll.

  16. See What How Much Is A Private ADHD Assessment UK Tricks The Celebs Are Utilizing How Much Is A Private Adhd Assessment Uk

  17. Frankevild表示:

    https://rybelsus.shop/# cheapest rybelsus pills

  18. Hey! I realize this is sort of off-topic but I
    needed to ask. Does building a well-established website like yours require a lot of work?
    I’m brand new to writing a blog but I do write in my diary on a daily basis.
    I’d like to start a blog so I can easily share my own experience
    and thoughts online. Please let me know if you have any ideas or tips for brand new aspiring bloggers.
    Appreciate it!

  19. 15 Amazing Facts About Coffee Machines From Bean To Cup That You Didn’t Know Bean to cup coffee machine home – http://demo01.Zzart.me/,

  20. RamonRek表示:

    cheapest rybelsus pills: buy rybelsus online – semaglutide tablets

  21. Cliff Badham表示:

    The Intermediate Guide On Beans Coffee Machine which bean to cup coffee machine (https://lovelydirectory.com/)

  22. Jeremypag表示:

    rybelsus coupon semaglutide tablets semaglutide tablets

  23. вывод из запоя на дому ростов круглосуточно вывод из запоя на дому ростов круглосуточно .

  24. Seven Reasons Why Bandar Toto Is Important Toto Online Terbaik; Maps.Google.Cat,

  25. BTC Boost Max表示:

    You really make it seem so easy with your presentation but I find this matter to be really something that I
    think I would never understand. It seems too complicated and extremely broad for
    me. I am looking forward for your next post, I’ll try to get the hang of it!

  26. В магазине сейфов предлагают сейфы москва купить сейф в москве цена

  27. Hi there, just wanted to say, I enjoyed this post. It was helpful.

    Keep on posting!

  28. It’s amazing to pay a visit this site and reading the views of all colleagues concerning this article,
    while I am also keen of getting experience.

  29. G76toto表示:

    Good post. I will be experiencing some of these issues
    as well..

發佈留言

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