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,393,267 Responses

  1. Elmer Forsyth表示:

    Seriously quite a lot of superb material!

    Feel free to visit my site; https://hot-licks.org

  2. sitemap.xml表示:

    Thasnk youu ffor sharing your thoughts. I tfuly appreciaste your efforts annd I am waitin for yopur further write upps thank yoou
    onc again.

  3. Cliff表示:

    %%

    My page: vauxhall astra key replacement – Cliff

  4. sitemap表示:

    Whhen I oruginally commenged I clicked the
    “Notify me when new comments are added” checkbbox andd now
    each time a comkment iss added I geet several e-mails wwith tthe ssme comment.
    Is there any way you ccan remove people from that service?
    Thanks!

  5. sitemap表示:

    My brother suggested I migtht like this web site.
    He waas entirdly right. Thiis posst actully mmade myy day.

    Youu ccan not imagine just howw much time I haad spent for this information! Thanks!

  6. I just like the helpful info you supply to your articles.
    I will bookmark your weblog and test once more right here regularly.
    I am fairly sure I will be informed a lot of new stuff proper
    right here! Good luck for the following!

  7. I’m really enjoying the design and layout of your blog.

    It’s a very easy on the eyes which makes it much more enjoyable for me to
    come here and visit more often. Did you hire out a developer to create your theme?
    Superb work!

    My web page Tiny House Community

  8. google ads表示:

    I couldn’t resist commenting. Exceptionally well
    written!

  9. Randall表示:

    %%

    Here is my website … volvo key fobs, Randall,

  10. sitemap.xml表示:

    My family every tije ssay that I am wastjng my time here att net,
    except I know I am getting experiennce all thhe time by reading such nice posts.

  11. Chastity表示:

    window doctor brentwood – Chastity,

  12. Williamlooda表示:

    https://sildenafil.win/# generic sildenafil nz

  13. Are you tired of limitations on your Cash App transactions?
    Look no further, buy a verified Cash App account today and enjoy
    unrestricted usage. Avoid the hassle of verification processes and enjoy the convenience of seamless transactions.
    Say goodbye to limitations and start unlocking the full potential of your Cash App
    with a verified account.

  14. sitemap.xml表示:

    I waas curious if yoou evedr thoight oof chawnging tthe strducture off your site?
    Its very wewll written; I ove wht youve goot to say. Butt
    maube youu coulld a littloe more inn tthe way oof content
    soo people could connect wuth iit better. Yove got ann awcul llot of texdt for only havijng one or 2 pictures.
    Maybe yoou could space itt outt better?

  15. te.legra.ph表示:

    upvc window repairs leatherhead (te.legra.ph)

  16. 10 Pinterest Accounts To Follow About Pink homecoming dresses.Homecoming
    Dresses

  17. If you’re looking to buy a verified Cash App account,
    it’s essential to consider several factors before
    making a decision. Buying a verified account ensures added security, trust, and credibility.
    However, caution must be exercised to avoid scams or fraudulent sellers.

    Research thoroughly, read reviews, and choose a reputable seller
    who can guarantee the authenticity and legitimacy of the Cash App account.
    Protect your financial information and enjoy the convenience of a
    verified Cash App account safely.

  18. Howdy! This blog post couldn’t be written any better!
    Looking at this article reminds me of my previous roommate!
    He continually kept talking about this. I most certainly will send this information to him.
    Pretty sure he’ll have a good read. Thank
    you for sharing!

  19. buy表示:

    %%

    Here is my webpage buy

  20. Are you tired of dealing with the hassle of unverified Cash
    App accounts? Look no further – buying a verified Cash App account can be your ultimate solution.
    Enjoy seamless transactions with the assurance of verified identity and added security.
    Get your hands on a verified Cash App account today and experience a whole new level of
    convenience and peace of mind. Don’t miss out on this opportunity
    – secure your transactions now!

  21. sitemap.xml表示:

    I paay a visit each day sme web pages annd information sites tto rezd articlles oor reviews,
    however tthis weboage presents quality based posts.

  22. Do you have a spam problem on this site; I also am a blogger, and
    I was wondering your situation; we have created some nice procedures and we are looking to trade solutions with others, why
    not shoot me an email if interested.

  23. %%

    Feel free to visit my web-site … mesothelioma asbestos law firm (http://www.google.com)

  24. Just simply desired to emphasize I am just thrilled that i happened
    in your page.
    https://warszawa24.ovh

發佈留言

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