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,415,327 Responses

  1. Hey there, You’ve done a great job. I’ll certainly digg it and personally
    suggest to my friends. I am sure they’ll be benefited from this web site.

  2. artmisblog.ir表示:

    Wonderful items from you, man. I’ve be aware your stuff previous to
    and you’re simply extremely magnificent. I really like what you have obtained right
    here, certainly like what you are stating and the way during which
    you say it. You’re making it entertaining and you continue to take care of to
    keep it wise. I cant wait to read much more from you. This
    is really a great web site.

  3. Dustin表示:

    Some degree programs demand an internship, but if yours doesn’t, attempt to match an internship in if you can.

    Look into my page :: Dustin

  4. Savannah表示:

    Even though the FLSA places limits on working hours, it doesn’t specify minimum limits.

    Stop by my homepage … Savannah

  5. Wоw, amazing blog layout! How long have you ever been blogging for?
    you made blogging look easy. The total look of your website is magnificent, as smartly as the content material!

  6. slotpg表示:

    I am not sure where you’re getting your info, but good topic.

    I needs to spend some time learning more or understanding more.
    Thanks for great info I was looking for this information for my mission.

  7. %%

    Also visit my web site: spf30 Moisturiser

  8. If you would like to increase your familiarity simply keep visiting this site and be updated with the most
    up-to-date gossip posted here.

  9. Look at how much details there may be concerning your personalized Don’t Worry Darling film online.
    You may be able to climb up the Search engine optimisation search rankings if you are in a web
    site relating to your Don’t Worry Darling movie. This will provide you with produce a tiny funds to place
    towards your Don’t Worry Darling movie.

  10. obzorgame.com表示:

    Maintain the exceptional job !! Lovin’ it!

  11. hacks表示:

    Oh my goodness! Awesome article dude! Many thanks, However I am
    having difficulties with your RSS. I don’t understand why I am unable to join it.

    Is there anybody having the same RSS problems? Anybody who knows the answer will
    you kindly respond? Thanx!!

  12. WOW just what I was looking for. Came here by searching
    for gold price per gram in germany today https://my.desktopnexus.com/wazayif333/

  13. mbti表示:

    %%

    Review my website … mbti

  14. Boyce表示:

    PayPal operates like a checking account, with customers depositing funds into their PayPal account byy way off banbk transfer, debit card,
    or credit card.

    My blog … vegas online casino – Boyce

  15. More info表示:

    Hey I know this is off topic but I was wondering if you knew of
    any widgets I could add to my blog that automatically
    tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time
    and was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new updates.

  16. dating chat表示:

    I was suggested this blog via my cousin. I am nott sure whether or not this publish is written via himm as nobody else understand such distinctive about my
    trouble. You are incredible! Thank you!

  17. Hi friends, how is all, and what you desire to say regarding this post, in my
    view its really awesome for me.

  18. If you wish for to improve your know-how simply keep visiting this web page and be
    updated with the most recent news update posted here.

  19. I’m not that much of a internet reader to be honest but
    your sites really nice, keep it up! I’ll go ahead and bookmark
    your website to come back later on. Many thanks

  20. I was recommended this website by my cousin. I’m not sure whether this post is written by him
    as nobody else know such detailed about my trouble. You are wonderful!
    Thanks!

  21. I am extremely inspired along with your writing abilities as smartly as with
    the layout in your blog. Is that this a paid subject matter or did you customize it yourself?
    Anyway keep up the nice high quality writing, it is rare
    to see a nice blog like this one today..

  22. We absolutely love your blog and find a lot of your
    post’s to be precisely what I’m looking for. Does one offer guest writers to write content available for you?
    I wouldn’t mind producing a post or elaborating on many of the subjects you
    write related to here. Again, awesome weblog!

  23. I’m really impressed with your writing skills and also with the
    layout on your blog. Is this a paid theme or did you modify it yourself?
    Either way keep up the excellent quality writing, it’s rare to see a nice blog like this one
    nowadays.

  24. It is perfect time to make a few plans for the longer term and it’s time to be happy.
    I’ve read this put up and if I could I want to counsel you few interesting issues
    or tips. Perhaps you can write subsequent articles referring to this
    article. I wish to read more things approximately it!

  25. Big five表示:

    %%

    Feel free to surf to my blog Big five

發佈留言

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