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,147,651 Responses

  1. 14 Common Misconceptions Concerning Certified Slots Online slots – olderworkers.com.au

  2. nuriteck.com表示:

    Slot Tours: The Secret Life Of Slot Tours hacksaw slots Online casino (nuriteck.com)

  3. rape porn表示:

    I’m not sure why but this blog is loading very slow for
    me. Is anyone else having this problem or is it a problem on my end?

    I’ll check back later and see if the problem still exists.

  4. From All Over The Web Twenty Amazing Infographics About Online Shopping Sites In United Kingdom 5×40 white gloss car wrap

  5. Thanks designed for sharing such a pleasant opinion, piece of writing is nice,
    thats why i have read it entirely

  6. Slot Strategy表示:

    Five Slot Strategies Lessons From Professionals Slot Strategy

  7. I think this is one of the most important information for
    me. And i am glad reading your article. But wanna remark on few general things, The web site
    style is great, the articles is really great : D. Good job,
    cheers

  8. My brother suggested I might like this blog. He was entirely right.

    This post truly made my day. You cann’t imagine just how much time I had spent for this information! Thanks!

  9. I was recommended this website by my cousin. I’m not certain whether this post is written by him as no one else recognize such specific about
    my difficulty. You’re wonderful! Thank you!

  10. Modern Slots表示:

    The Reason Why You’re Not Succeeding At Classic
    Casino Slots Modern Slots

  11. Double Glazing Near Me: What No One Has Discussed double Glazing window handles

  12. 10 Misconceptions Your Boss Has Concerning Window Screen Replacement Replacement Window

  13. Source表示:

    Yes! Finally someone writes about Source.

  14. Undisputed Proof You Need Double Glazing Fitters Near Me double glazing installers

  15. It’s amazing to pay a visit this web page and reading the views of all colleagues on the topic of this post, while I am also zealous of getting
    experience.

  16. The Most Significant Issue With Upvc Window Repairs, And How You Can Solve
    It Upvc Window Repairs Near Me

  17. The 12 Most Popular Jackpot Slots Accounts To Follow On Twitter
    video poker slots

  18. 15 Gifts For The Slot Demo Pragmatic Lover In Your Life pragmatic play demo

  19. Бесплатная программа Wasender 1500 –
    рассылка в whatsapp для массовой рассылки в
    ватсап

  20. Preventing burst pipes is always the best way tto lower repair costs.

  21. Since the admin of this website is working, no question very
    soon it will be famous, due to its feature contents.

  22. Drug information leaflet. Drug Class.
    buying azathioprine prices
    Some about medicine. Read information here.

  23. porn表示:

    My coder is trying to convince me to move to .net
    from PHP. I have always disliked the idea because of the
    costs. But he’s tryiong none the less. I’ve been using WordPress on a variety of websites for about
    a year and am anxious about switching to another platform.

    I have heard fantastic things about blogengine.net.
    Is there a way I can transfer all my wordpress posts into it?
    Any kind of help would be greatly appreciated!

  24. Ten New Slots Online-Related Stumbling Blocks You
    Shouldn’t Post On Twitter land-Based slots

  25. Case gamma表示:

    Responsible For A CS GO Cases To Open Budget?
    12 Ways To Spend Your Money Case gamma

  26. How The 10 Worst Online Sites For Shopping In Uk-Related FAILS Of All Time Could Have Been Prevented green striped motorized awning

  27. Diplom_lrKl表示:

    купить аттестат ukr-diplom.ru .

發佈留言

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