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,364,326 Responses

  1. My brother recommended I might like this blog. He was entirely right.
    This post actually made my day. You cann’t imagine simply how much time I had spent for
    this info! Thanks!

  2. %%

    Here is my homepage – ghost immobiliser near Me (Fuchsia-Camel-fhb8B3.mystrikingly.Com)

  3. shop表示:

    %%

    Here is my blog post; shop

  4. Wow, amazing weblog structure! How long have you ever been running a
    blog for? you made running a blog glance easy. The overall glance of your site is
    wonderful, as neatly as the content material!

  5. When someone writes an article he/she retains the image of a
    user in his/her mind that how a user can understand it.
    Therefore that’s why this article is perfect. Thanks!

  6. service表示:

    %%

    My website – service

  7. If you take a long minute working to improve your content and you are able to have a high approval rate.

    my web blog … https://claytonfsdnx.wikimillions.com/2764862/gsa_ser_link_building_mastery

  8. Lenora表示:

    %%

    Here is my website :: jaguar x type key fob replacement (Lenora)

  9. Andreas表示:

    %%

    Also visit my web blog: rabbit vibrators for sale (Andreas)

  10. Kandis表示:

    specializes in asbestos litigation (Kandis)

  11. What’s up every one, here every one is sharing such know-how,
    thus it’s fastidious to read this weblog, and I used to go to see this weblog every day.

  12. Daryl表示:

    %%

    My webpage – saab 9-3 replacement key (Daryl)

  13. %%

    Visit my homepage; uk representative (pa.biznet-us.com)

  14. Frances表示:

    glazing kingston (Frances)

  15. Boats表示:

    Whoa! This blog looks exactly like my old one!
    It’s on a entirely different subject but it has pretty much the same layout and design. Great choice of
    colors!

  16. If you’re looking to securely transact online, consider buying a verified Cash App account.
    With verified accounts, you can confidently send and receive money, shop online, and even invest.
    Enjoy added security and peace of mind with your financial transactions.

    Don’t miss out on the convenience and safety of a genuine
    Cash App account.

  17. I just could not leave your site prior to suggesting that I extremely loved the standard information an individual provide to your guests?
    Is going to be back steadily in order to check up on new posts

  18. Are you tired of facing restrictions on your Cash App account?
    Buy a verified Cash App account and enjoy uninterrupted transactions.
    Get instant access to all features and bid farewell
    to account limitations. Don’t miss out on this opportunity – buy
    your verified Cash App account today!

  19. Hey There. I found your blog using msn. This is a
    very well written article. I will be sure to bookmark it and come
    back to read more of your useful information. Thanks for the post.

    I will definitely comeback.

  20. Telegra.Ph表示:

    %%

    Here is my blog: Cheap Double Glazing Milton Keynes (Telegra.Ph)

  21. Are you in need of a verified Cash App account? Look no further!
    Buying a verified Cash App account is the best solution for hassle-free online transactions.
    With a verified account, you can enjoy increased transaction limits, secure payments, and peace
    of mind. So why wait? Get your hands on a
    verified Cash App account today and experience the convenience
    of digital payments like never before!

發佈留言

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