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,400,526 Responses

  1. sex near me表示:

    It’s amazing to pay a visit this site and reading the views
    of all mates regarding this piece of writing, while I am also eager of getting experience.

    my blog … sex near me

  2. Paragraph writing is also a excitement, if you be familiar with after that
    you can write otherwise it is complicated to write.

  3. hi!,I love your writing so a lot! share we keep in touch extra approximately your post on AOL?
    I need a specialist on this area to solve my problem.
    Maybe that is you! Having a look ahead to see you.

  4. Ԝe are a grߋup of volunteers aɑnd starting a brand new scheme in our ϲommunity.
    Your site proᴠided uss ѡith useful nformation to work on. You’ve done a formidale
    taѕk and our entire neіghbоrhood ԝikl
    be thankful to you.

    Also visit my blog :: Các loại đồng hồ nữ

  5. ytmp3.li表示:

    I’m very happy to uncover this page. I need to to thank you for your time just
    for this wonderful read!! I definitely appreciated every part of it and I have you book marked to look at
    new things on your web site.

  6. %%

    Here is my site: csgo case battle sites, https://click4r.Com/,

  7. My relatives every time say that I am killing my time here at net, but
    I know I am getting knowledge every day by reading thes nice articles.

  8. %%

    Also visit my web page; uk cbd oil (http://www.knitandpurl.co.kr)

  9. asslick表示:

    Αmazing thingѕ here. I’m very gpad tto seee your post.
    Thank you soo much aand I aam taking a look ahead to touch you.
    Will you ҝidly drop mme a e-mail?

  10. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you!
    However, how can we communicate?

    Take a look at my blog poker site rankings – 40th.jiuzhai.com,

  11. near表示:

    %%

    Feel free to surf to my website near

  12. The People Closest To Washing Washer Dryer combinations Combo
    Share Some Big Secrets

  13. Angie表示:

    %%

    Also visit my site :: replacement skoda key (Angie)

  14. telegra.ph表示:

    %%

    Feel free to surf to my web blog; csgo Case websites – telegra.ph,

  15. Billy表示:

    windows and doors kingston, Billy,

  16. Wow that was strange. I just wrote an really long comment
    but after I clicked submit my comment didn’t show up.
    Grrrr… well I’m not writing all that over again. Anyhow,
    just wanted to say excellent blog!

  17. Hello there, I think your site may be having web
    browser compatibility problems. Whenever I take
    a look at your web site in Safari, it looks fine however, when opening
    in I.E., it’s got some overlapping issues. I merely wanted to give you a quick heads
    up! Besides that, fantastic site!

  18. In order to increase university ranking in webometrics,
    proper SEO strategy is needed. Webometrics Jakarta’s
    SEO services can help improve the ranking of university sites through
    improving the quality of content and site visitors.
    This can be done through optimizing efforts from the inside of the content and
    outside of the content. Contact us at: https://bit.ly/jasawebometrics2023

  19. Charis表示:

    That is very fascinating, You’re an overly skilled blogger.

    I have joined your rss feed and stay up for searching for
    more of your excellent post. Also, I have shared your web site in my
    social networks

    Here is my web blog; facebook local sex – Charis,

  20. Hello there! Would you mind if I share your blog with my myspace group?
    There’s a lot of folks that I think would really enjoy your content.

    Please let me know. Thanks

  21. fob表示:

    %%

    My web page; fob

  22. Are you tired of the hassle that comes with creating a Cash
    App account? Look no further! Buy a verified Cash App account and experience smooth transactions and hassle-free money management.

    With a verified account, you can enjoy added security and unlock additional
    features. Don’t miss out on this opportunity, get your
    verified Cash App account today!

  23. I am regular visitor, how are you everybody? This article posted at this website is in fact
    nice.

  24. free standing electric fireplaces (m.neol.piece-worker.Com)

  25. Are you tired of dealing with the hassles of getting a verified Cash
    App account? Look no further! We offer the opportunity to buy a verified account hassle-free.
    With a verified account, you can enjoy the full benefits
    of Cash App without any limitations. Don’t waste time, get your verified Cash App account now!

發佈留言

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