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,247,020 Responses

  1. BernardMop表示:

    order cytotec online buy cytotec cytpremium cytotec abortion pill

  2. DarrenUnjuh表示:

    https://cytpremium.com/# buy cytotec online fast delivery

  3. Ada Mcelmury表示:

    Residential Land become limited

  4. Let’s take on the amazing challenges of slots together at yo088; it’s time for some fun! Yo88

  5. Medicament information for patients. Drug Class.
    metoprolol thiazide combination
    Some information about meds. Read information here.

  6. Does anybody know if I can lease a pressure washer exterior cleaning

  7. Residential Development become further away from City or Establish Town

  8. DarrenUnjuh表示:

    https://cytpremium.com/# cytotec pills buy online

  9. I feel that is one of the so much important info for me. And i am satisfied reading your article.

    However should statement on few basic things, The site style is
    perfect, the articles is in reality great : D. Good process, cheers

  10. AlbertDuM表示:

    buy Clopidogrel over the counter: Plavix Clo – antiplatelet drug

  11. Hello I am so grateful I found your site, I really found you by error,
    while I was looking on Yahoo for something else, Anyways I am
    here now and would just like to say thanks for a marvelous post and a all round entertaining blog (I
    also love the theme/design), I don’t have time
    to read through it all at the minute but I have bookmarked it and also added your RSS feeds, so when I
    have time I will be back to read a great deal more, Please do keep up
    the great job.

  12. BrandenSlomy表示:

    amoxicillin 500mg capsules price: amoxil com pharm – amoxicillin price canada

  13. reputation表示:

    First of all I want to say excellent blog! I had a quick question that
    I’d like to ask if you do not mind. I was interested to find out how you center yourself
    and clear your head prior to writing. I have had trouble clearing
    my thoughts in getting my ideas out. I truly do enjoy writing however it just seems
    like the first 10 to 15 minutes are generally wasted
    simply just trying to figure out how to begin. Any recommendations
    or hints? Kudos!

  14. After I initially left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is
    added I get 4 emails with the same comment. Perhaps there is a way you can remove me
    from that service? Thanks a lot!

  15. BrandenSlomy表示:

    where to buy amoxicillin pharmacy: Amoxicillin Com Pharm – cost of amoxicillin

  16. BernardMop表示:

    buy cytotec pills cytpremium purchase cytotec

  17. We’re a gaggle of volunteers and opening a brand new scheme in our community.
    Your site provided us with valuable information to work on. You’ve done a formidable process
    and our entire neighborhood will be thankful to you.

  18. You can start the process by copying the URL link of the video and then pasting it
    on the downloader window.

  19. Νice post. I was checking continuously this blog and I’m impressed!
    Extfemely useful info speciaⅼlү the last part 🙂 І care fоr such infoгmatiοn much.
    I was seeking this certain іnformation for a very long time.
    Thank ʏou and best of luck.

    Feel free to ѕurf to my homepage: Túi xách nữ

  20. Dean King表示:

    I appreciate how flexible P&J Cleaners is with scheduling vancouver maid service

  21. I’m really loving the theme/design of your website.
    Do you ever run into any internet browser compatibility
    problems? A small number of my blog visitors have complained about my blog not working correctly
    in Explorer but looks great in Safari. Do you have any solutions to help
    fix this issue?

  22. BrandenSlomy表示:

    can i get clomid prices: buy clomid – can i get clomid prices

  23. Nikefut表示:

    dark market 2024 tor darknet dark website

發佈留言

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