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...

633,338 Responses

  1. Hello mates, its wonderful article regarding cultureand entirely explained,
    keep it up all the time.

  2. HansNer表示:

    safe list of darknet market links steroid market darknet

  3. Klaudia Hall表示:

    THC isolate is pure THC gummies france,
    with no other cannabinoids.

  4. Its such as you read my thoughts! You appear to grasp so much approximately this,
    such as you wrote the book in it or something. I believe that you could do with some % to force the message house a bit, but instead of that,
    this is excellent blog. A fantastic read. I will certainly be back.

  5. sexdoll Sale表示:

    %%

    Feel free to surf to my web site; sexdoll Sale

  6. Sports betting. Bonus to the first deposit up to 500 euros.
    Online Casino.
    sports betting

  7. Mellissa表示:

    %%

    Also visit my page – how to find investors in south africa (Mellissa)

  8. I have been browsing online more than 3 hours today, yet I
    never found any interesting article like yours.
    It’s pretty worth enough for me. In my view, if all site owners and bloggers made good content as you did, the
    net will be much more useful than ever before.|
    I could not refrain from commenting.
    Perfectly written!|
    I will right away grasp your rss feed as I can not to find your email subscription link or
    newsletter service. Do you have any? Please allow me recognize in order
    that I may just subscribe. Thanks. |
    It is appropriate time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I wish to suggest you some
    interesting things or suggestions. Perhaps you can write next articles referring to this article.

  9. SMTP server表示:

    My spouse and I stumbled over here from a different page and thought I might as
    well check things out. I like what I see so now i am following you.

    Look forward to finding out about your web page again.

  10. Definitely imagine that that you stated. Your favorite justification appeared to be at the web the simplest
    thing to take note of. I say to you, I definitely get annoyed while folks think
    about worries that they just don’t recognize about. You controlled to hit the nail upon the top and defined out the
    entire thing without having side-effects , people can take
    a signal. Will probably be again to get more. Thanks

  11. Madie表示:

    It’s appropriate time to make some plans for the future and
    it is time to be happy. I have read this post and if I could I wish to
    suggest you some interesting things or tips.
    Maybe you can write next articles referring to this article.
    I want to read even more things about it!

    Take a look at my page; avon kosmetik online shop, Madie,

  12. Madge表示:

    %%

    Here is my web site: broken glass repair – Madge,

  13. roof repairs表示:

    You Need Roofing Contractors?

  14. And also the range of various THC gummies montreal items.

  15. rpoforums.com表示:

    If you are going for finest contents like me, simply pay a visit this web
    site every day for the reason that it offers feature contents, thanks

    my website; avon shop online (rpoforums.com)

  16. Williamjug表示:

    darknet black market sites tor2door market

發佈留言

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