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,432,032 Responses

  1. Looking to buy a verified Cash App account?
    Verified accounts offer added security and increased transaction limits.
    Don’t miss out on the opportunity to have a hassle-free and reliable payment platform.

    Buy a verified Cash App account today and enjoy the convenience it offers.

  2. When I initially commented I appear to have clicked the -Notify
    me when new comments are added- checkbox and from now on every time a
    comment is added I recieve four emails with the same comment.
    There has to be a way you can remove me from that service?
    Kudos!

  3. Bess表示:

    austin asbestos attorney [Bess]

  4. Keenan表示:

    %%

    my blog post :: double glazed window chelmsford – Keenan

  5. What’s up it’s me, I am also visiting this web page regularly,
    this web site is truly good and the people are truly sharing pleasant thoughts.

  6. Looking to buy a verified Cash App account?
    Look no further! With a verified account, you can enjoy added security, increased
    transaction limits, and seamless money transfers. Don’t
    settle for less, get a verified Cash App account today and enjoy hassle-free
    financial transactions.

  7. legal cbd表示:

    %%

    My blog post :: legal cbd

  8. You reported it effectively!|
    Amazing loads of valuable data!|
    Nicely put. Many thanks.|
    Very well spoken truly! !|
    Fine info, Cheers!|
    Cheers! I enjoy this.|
    Many thanks, Lots of advice!
    Free Fire India APK

  9. Theo表示:

    %%

    Feel free to surf to my site … delta-8 thc (Theo)

  10. Gus表示:

    asbestos claims lawyers (Gus)

  11. Εverything іs vеry оpen ᴡith a precise explanation оf thе challenges.
    It was trսly informative. Уour website is verү helpful.
    Manny tһanks fоr sharing!

    Visit my homеpage: beli followers ig aktif indonesia

  12. Everything is very open with a precise explanation of the issues.
    It was definitely informative. Your website is useful.
    Many thanks for sharing!

  13. %%

    Feel free to surf to my website saab keys (http://www.jj0bn2yi8dwpp.com)

  14. If you’re looking for a hassle-free way to buy and sell online, consider getting a verified Cash App account.
    With a verified account, you can easily transfer money, make secure payments, and
    enjoy added benefits such as increased transaction limits.
    Don’t miss out on the convenience and peace of mind that a verified Cash App account brings.
    Get yours today!

  15. %%

    Feel free to visit my web blog; Headphones Wireless

  16. Interested in buying a verified Cash App account?
    Look no further! We offer reliable and verified accounts to ensure a smooth and secure transaction experience.
    Say goodbye to account limitations and start enjoying the benefits of a verified Cash App account today!

  17. Are you tired of dealing with the hassles of unreliable payment platforms?
    Look no further! Buy a verified Cash App account and enjoy hassle-free
    transactions. With a verified account, you can securely send, receive, and store
    money with ease. Don’t settle for less when it comes to your financial transactions.

    Invest in a trusted Cash App account today!

  18. Hеya! I’m at work brlwsing your blog from my new iphone 4!
    Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the outstanding wоrk!

  19. Antonia表示:

    %%

    Look at my blog post :: cbd oil broad spectrum (Antonia)

  20. Wow, amazing blog layout! How long have you been running
    a blog for? you make blogging look easy. The overall look of your
    website is fantastic, let alone the content!

  21. emphysema caused by railroad how to get a settlement (Marvelcomics.faith)

  22. quality表示:

    %%

    Take a look at my site … quality

  23. Cassie表示:

    It’s actually a great and helpful piece of information. I’m happy that you
    simply shared this useful info with us. Please keep us up to date like this.
    Thanks for sharing.

    Here is my blog; slots online – Cassie

  24. telegra.ph表示:

    stomach cancer caused by railroad how To Get a settlement (telegra.ph)

  25. Are you tired of dealing with unverified Cash App
    accounts? Upgrade your experience with a verified account today!

    Avoid the hassle of payment restrictions and ensure secure transactions.
    Don’t miss out on the benefits of a trusted Cash App account – buy yours now!

發佈留言

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