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,429,821 Responses

  1. Hello, i believe that i noticed you visited my web site
    so i came to return the desire?.I am attempting to to find issues to improve my site!I suppose its adequate to use a few of your ideas!!

    Feel free to visit my homepage … Off-Grid Tiny House

  2. %%

    Feel free to surf to my web page: replacement key for saab 93 (http://dhmine.co.kr)

  3. Excellent article. Keep writing such kind of info on your site.
    Im really impressed by your blog.[X-N-E-W-L-I-N-S-P-I-N-X]Hello there, You’ve done a great job.
    I’ll definitely digg it and in my view suggest to my friends.
    I’m confident they will be benefited from this web site.

    Also visit my web page :: local hookups near me

  4. This is my first time pay a visit at here and i am actually
    happy to read everthing at alone place.

  5. Bennie表示:

    %%

    Review my site – cbd oil for pets price uk (Bennie)

  6. hanamsil.kr表示:

    %%

    Also visit my web-site :: suzuki ignis key (hanamsil.kr)

  7. ALPHA X ULTRA表示:

    Superb blog! Do you have any suggestions for aspiring writers?

    I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you recommend starting with a free platform
    like WordPress or go for a paid option? There are so many choices out there that I’m
    completely overwhelmed .. Any ideas? Thank you!

  8. Can I just say what a comfort to uncover a person that genuinely knows what they
    are discussing over the internet. You definitely realize how to bring
    a problem to light and make it important.
    More and more people ought to check this out and understand this side of your story.
    I was surprised that you are not more popular given that you certainly have the gift.

  9. इनमें से किसी एक वेबसाइट पर क्लिक करें.

  10. 먹튀검증表示:

    Heya i’m for the primary time here. I came across this board and I
    in finding It truly useful & it helped me out much.

    I am hoping to provide one thing back and help others like you helped me.

  11. I don’t usually comment but I gotta admit regards for the post on this special
    one :D.

    Here is my web-site best online poker

  12. Program表示:

    %%

    Feel free to surf to my blog post; Program

  13. online表示:

    %%

    My webpage … online

  14. %%

    Feel free to surf to my website; replacement smart car key;
    legendsnkr.com,

  15. Hi it’s me, I am also visiting this website regularly, this site is actually fastidious
    and the viewers are genuinely sharing good thoughts.

  16. Carma表示:

    %%

    Feel free to surf to my blog post skoda key programming (Carma)

  17. 10 Things You Learned From Kindergarden That Will Help You With Washer Tumble Dryer Combo

    Stop by my site – Washing machine sale

  18. sex cam web表示:

    I’ve been browsing online more than 4 hours today, yet
    I never found any interesting article like yours.

    It is pretty worth enough for me. In my view, if all
    site owners and bloggers made good content as you did, the web will be much more useful than ever before.

  19. vak.kr表示:

    %%

    Check out my page :: volkswagen car key (vak.kr)

  20. sga508表示:

    Hi there Dear, are you truly visiting this website regularly, if so after that you
    will definitely get fastidious know-how.

  21. Greg表示:

    %%

    Visit my website … csgo cases new, Greg,

  22. Oh my goodness! Awesome article dude! Thank you so much, However I am encountering difficulties with your RSS.

    I don’t know the reason why I can’t join it. Is there anybody else getting similar RSS problems?
    Anyone who knows the solution can you kindly respond?
    Thanks!!

  23. I think the admin of this web page is genuinely working hard in support of
    his web site, for the reason that here every data is quality based stuff.

  24. Ilse McLean表示:

    Nicely put. Thanks.

  25. xanh表示:

    Thanks a lot for sharing this with all people you really understand what you’re speaking approximately!
    Bookmarked. Please also visit my site =). We can have a hyperlink exchange agreement
    among us!

  26. sitemap表示:

    Oh my goodness! Impressive article dude! Thak you,
    However I am going thdough troubles with yohr RSS.
    I don’t knmow wwhy I amm unale too jin it. Is thuere anybody lse getting the same RSS
    problems? Anyine who knows tthe solution cann yyou
    kindly respond? Thanx!!

  27. %%

    my homepage; renault replacement key, vn.Easypanme.com,

  28. sitemap表示:

    Havingg rea this I thought itt wass verry enlightening.
    I apprewciate you spending some time andd effort to puut thiis contennt
    together. I once agakn ffind mhself spending a significant anount oof timme botth readinmg andd commenting.
    Butt soo what, itt was stipl woreth it!

發佈留言

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