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,146,691 Responses

  1. Diplom_qqKl表示:

    купить сертификат специалиста http://ukr-diplom.ru .

  2. Diplom_ihKl表示:

    купить диплом медсестры купить диплом медсестры .

  3. First off I want to say terrific blog! I had a quick question which I’d like to ask if you don’t mind.

    I was curious to find out how you center yourself and clear your head before
    writing. I’ve had a tough time clearing my mind in getting my ideas out.

    I truly do enjoy writing however it just seems like the
    first 10 to 15 minutes are lost simply just trying to figure out how to begin. Any
    recommendations or hints? Kudos!

  4. Really no matter if someone doesn’t know afterward its up to other visitors
    that they will help, so here it occurs.

  5. What Do You Think? Heck Is Best Slot Machines? legitimate slots

  6. Unquestionably believe that which you said. Your favorite reason seemed to be on the internet the simplest thing to be aware of.

    I say to you, I definitely get annoyed while people consider worries that they
    plainly don’t know about. You managed to hit the nail upon the top and also defined
    out the whole thing without having side-effects , people can take a signal.

    Will probably be back to get more. Thanks

  7. The Best Tips You’ll Ever Receive On Semi Truck Legal semi truck accident lawsuits

  8. Cs20 Case表示:

    “The CSGO Battle Case Awards: The Top, Worst, Or Strangest Things We’ve Ever Seen Cs20 Case

  9. Cindi表示:

    We’re a bunch of volunteers and starting a new scheme in our community.
    Your site provided us with useful info to work on. You’ve done a formidable job and our
    whole community will probably be thankful to you.

  10. MAU777表示:

    Hey There. I found your blog using msn. This
    is a really well written article. I will be sure to bookmark it and return to
    read more of your useful information. Thanks for the post.
    I’ll certainly comeback.

  11. Five Tools That Everyone Within The Certified Slots Industry Should
    Be Making Use Of slots with Bonuses (http://www.e10100.com)

  12. Avery表示:

    The Most Common Slot Updates Debate Could Be As Black And
    White As You Might Think slots with free spins (Avery)

  13. fit表示:

    You’ll Never Be Able To Figure Out This Treadmills
    UK’s Benefits fit

  14. Great postings, Regards.

  15. Guide To Dangerous Drugs Law Firm: The Intermediate Guide For
    Dangerous Drugs Law Firm dangerous drugs Law firm

  16. I need to to thank you for this excellent read!! I absolutely loved
    every little bit of it. I have you saved as a favorite to check out new things you post…

  17. davidofcigs表示:

    What’s up, I wish for to subscribe for this web site to take
    newest updates, therefore where can i do it please assist.

  18. 20 Trailblazers Leading The Way In Secure Slots slot Offers; http://www.google.com.pk,

  19. Repairs To Upvc Windows: 11 Thing You’re Not Doing window repair (http://www.qishuashua.com.cn)

  20. The Most Underrated Companies To In The Motor Vehicle Litigation Industry Motor Vehicle Accident Attorneys

  21. The 10 Most Terrifying Things About Slot Promotions
    Slot Promotions

  22. gatot Kaca表示:

    14 Savvy Ways To Spend Leftover Gatotkaca Slot Budget gatot Kaca

  23. This Is The One Slot Walk-Throughs Trick Every Person Should Be Aware Of casino slot tournaments

  24. rape porn表示:

    Hi friends, its impressive post on the topic of tutoringand fully explained, keep it up all the time.

  25. Thanks for your marvelous posting! I actually enjoyed reading it, you may be a great author.

    I will make sure to bookmark your blog and may come back
    at some point. I want to encourage you to ultimately
    continue your great writing, have a nice holiday weekend!

  26. This Is The New Big Thing In Free Slots Slot machine reviews

  27. Seven Reasons Why Motor Vehicle Settlement Is So Important
    motor vehicle accidents

發佈留言

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