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,367,802 Responses

  1. bandar 47表示:

    Thanks for another fantastic article. Where else may anyone get that type of information in such
    an ideal means of writing? I have a presentation next week, and I am
    at the search for such information.

  2. If you’re looking for a reliable way to buy and sell online, a verified Cash App account is the answer.
    With verified status, you gain access to enhanced security features
    and the ability to send and receive higher transaction limits.
    Don’t miss out on the convenience and peace of mind that a verified
    Cash App account offers. Get yours today!

  3. %%

    Feel free to visit my site :: cs battle case (https://www.regs.rw/)

  4. Hey there! I know this is somewhat off-topic however I
    needed to ask. Does running a well-established blog such as yours take a massive amount work?

    I’m completely new to operating a blog however I do write in my diary every day.
    I’d like to start a blog so I can easily share my own experience and thoughts online.
    Please let me know if you have any kind of ideas or tips for brand new aspiring blog
    owners. Appreciate it!

  5. Josefina表示:

    double glazing in corby, Josefina,

  6. Здесь можно не дорого и качественно оформить Справка 095 у

  7. boob表示:

    Fatаstіc beat ! I wish to apρrentice while you amend your site, hoᴡ caan i subscribe for a blog ᴡeb site?

    The accοunt helped me a ɑcceptable deal. I һad been a little bit acquainted of this your broadcaѕt proνidsed bright clеar concept

  8. Vamos ler este guia definitivo de como fazer campanhas do TikTok e aprender com os 10 melhores exemplos do TikTok.

  9. efficient表示:

    A Comprehensive Guide To Washing Machines Built In. Ultimate Guide To Washing
    Machines Built In

    My web blog: efficient

  10. You have very nice post and pictures, please have a look at
    our photo tours in the temples of Angkor
    gob.ar

  11. Scot Midgette表示:

    You said this terrifically.

  12. Ralph表示:

    %%

    Here is my blog post … buy full spectrum cbd
    oil [Ralph]

  13. Lyle表示:

    upvc windows berkhamsted (Lyle)

  14. I am regular reader, how are you everybody? This piece
    of writing posted at this web site is genuinely good.

  15. Hi friends, how is the whole thing, and what you wish for to say about this paragraph,
    in my view its genuinely remarkable in support of me.

  16. Yolanda表示:

    %%

    Feel free to surf to my web page – double glazed windows hackney (Yolanda)

  17. %%

    Also visit my web site … reps uk (m.En.thesuperplay.com)

  18. Gustavo表示:

    Don’t Make This Mistake With Your Customize Your custom wedding dresses (Gustavo) Dress

  19. Maximo表示:

    %%

    my page :: size 7 for shoes (Maximo)

  20. Brigida表示:

    %%

    Feel free to surf to my homepage :: real life silicone love dolls
    (Brigida)

  21. Nicely put. With thanks!

發佈留言

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