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,266,253 Responses

  1. It’s perfect time to make some plans for the long run and it is
    time to be happy. I have read this post and if I may I desire
    to suggest you some fascinating things or suggestions.

    Perhaps you could write next articles regarding this article.
    I desire to read even more things approximately it!

  2. Eula Colon表示:

    For any individual uncertain concerning just how to take care of big amounts of waste, leasing a dumpster is the method to go! Have a look at dumpster rental near Oak Ridge

  3. Jamesdiure表示:

    pinup-kazi.ru: пин ап казино – pinup

  4. DarioTob表示:

    пин ап вход: пинап казино – пинап казино

  5. Josephmax表示:

    https://vavada-kazi.ru/# vavada-kazi.ru

  6. penipuan表示:

    hello!,I love your writing very much! proportion we communicate extra about your article on AOL?
    I require an expert in this area to unravel my problem.
    May be that is you! Having a look forward to look you.

  7. Meds information sheet. Cautions.
    buy generic singulair without insurance
    Everything about medicament. Get information here.

  8. phising表示:

    My brother suggested I might like this blog. He was entirely right.
    This post truly made my day. You can not imagine just how much time I had
    spent for this information! Thanks!

  9. Jamesdiure表示:

    пинап казино: пин ап кз – пин ап казино онлайн

  10. Strong arguments here! Purchasing security can lead to long-lasting expense savings and comfort TreeStone Security Services

  11. Hi there, I enjoy reading through your post. I like to
    write a little comment to support you.

  12. DarioTob表示:

    пин ап вход: pinup – пин ап зеркало

  13. DarioTob表示:

    pinup: pinup-kazi.kz – пин ап казино онлайн

  14. Jamesdiure表示:

    казино вавада: вавада казино онлайн – vavada

  15. Aborsi表示:

    It’s actually a great and helpful piece of information.
    I am glad that you shared this useful information with us.

    Please stay us up to date like this. Thanks for sharing.

  16. Nikefut表示:

    deep web markets darknet websites tor markets links

  17. 10 Healthy Habits For Replace Volvo Key volvo Xc60 replacement Key

  18. I blog quite often and I seriously thank you for your information. The article has truly peaked my interest.
    I am going to book mark your website and keep checking for new details about once
    a week. I opted in for your Feed as well.

  19. Medicine information leaflet. Brand names.
    how to make codeine promethazine syrup
    All trends of drugs. Read information here.

  20. DarioTob表示:

    пин ап вход: pinup – пин ап зеркало

  21. Does your blog have a contact page? I’m having problems locating it but, I’d like to send you an email.
    I’ve got some suggestions for your blog you might be interested in hearing.

    Either way, great site and I look forward to seeing it develop over time.

    http://w4.prediksiangkakeramat.sbs/

  22. Simone表示:

    Here is my web page: Daycares popular listings – Simone,

  23. I will right away take hold of your rss feed as I can not find
    your email subscription link or e-newsletter service.

    Do you’ve any? Kindly let me understand in order that I could subscribe.
    Thanks.

  24. HenrydeT表示:

    вавада онлайн казино вавада казино онлайн vavada

  25. TKLMOPX9表示:

    When I originally left a comment I seem to have clicked
    the -Notify me when new comments are added- checkbox and now every
    time a comment is added I receive 4 emails with the exact same comment.
    Perhaps there is an easy method you are able to remove me from that service?
    Cheers!

  26. I visited various websites but the audio feature for audio songs current at
    this web page is truly superb.

    https://caramenggugurkankandunganherbal.com/

  27. Your means of telling all in this paragraph is actually fastidious,
    all be able to effortlessly be aware of it, Thanks a lot.

    my web page – timbangan Semarang (https://sapiterbang.weebly.com)

  28. DarioTob表示:

    pinup: pinup-kazi.ru – пин ап казино официальный сайт

發佈留言

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