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,363,228 Responses

  1. This site was… how do you say it? Relevant!! Finally I have found something which helped me.

    Thank you!

  2. Are you looking to enhance your online financial transactions?
    Buy a verified Cash App account today and experience hassle-free payments.
    With verified accounts, you can enjoy increased limits, added security, and peace of mind.
    Don’t miss out on this convenient and reliable way to handle your money.
    Get your verified Cash App account now!

  3. Guy表示:

    %%

    Check out my webpage; double glazing repair sutton (Guy)

  4. If you’re looking to buy a verified Cash App account, you’re in luck!
    Verified accounts offer added security and trust, ensuring smooth transactions and peace of mind.
    With a verified account, you can send and receive money without any worries.
    So, don’t wait any longer, get yourself a verified Cash App account today!

  5. http://www.videotrain.fr/

    Hey there just wanted to give you a quick heads up and let you know a few of the
    images aren’t loading properly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different web browsers and both show
    the same outcome.

  6. Unlock a seamless digital payment experience with verified Cash App
    accounts. Avoid limitations and enjoy unrestricted transactions.
    Buy now and simplify your financial transactions today!

  7. I am sure this paragraph has touched all the
    internet visitors, its really really good paragraph on building up new
    website.

  8. %%

    Take a look at my web site … Cbd Full Spectrum Uk
    (http://Intranet.Welfarebox.Com)

  9. Shirleen表示:

    upvc windows basingstoke (Shirleen)

  10. %%

    Here is my blog hemel hempstead electricians; Bookingsilo.Trade,

  11. Hi to all, how is all, I think every one is getting more from this site,
    and your views are fastidious designed for new viewers.

  12. %%

    Feel free to surf to my web blog: fridges for sale

  13. We are a bunch of volunteers and opening a brand new scheme in our community.
    Your website offered us with useful information to work
    on. You have done a formidable activity and our whole community will be thankful to you.

  14. %%

    Take a look at my webpage: Asbestos Lawsuit

  15. I got this web page from my pal who shared with me concerning this web
    page and now this time I am visiting this web site and reading very informative articles or reviews at this time.

  16. Dannielle表示:

    asbestosis settlement amounts (Dannielle)

  17. Lizzie表示:

    best mesothelioma lawyer pay (Lizzie)

  18. alfatv.Cz表示:

    online Designer handbags (alfatv.Cz)

  19. Reynaldo表示:

    %%

    Feel free to surf to my site: replacement windows redbridge (Reynaldo)

  20. If you’re looking to buy a verified Cash App account, it’s important to do
    your research and proceed with caution. While it may seem like a convenient
    option, there are risks involved. Make sure to verify the credibility of
    the seller, consider potential scams, and be aware that
    it violates Cash App’s terms and conditions. It’s always safer to create your own account
    or verify your existing one following the proper procedures.

  21. Why You Should Concentrate On Enhancing Laundry Washer And Dryer
    Combo

    Take a look at my site :: washing Machines

發佈留言

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