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,247,098 Responses

  1. Excellent web site you have got here.. It’s hard to find excellent writing like yours
    nowadays. I seriously appreciate individuals like you!

    Take care!!

  2. discuss表示:

    Right away I am ready to do my breakfast, afterward having my
    breakfast coming again to read more news.

  3. It’s awesome in support of me to have a website, which is helpful for my know-how.
    thanks admin

  4. https://s.id/表示:

    Hi there! I just wanted to ask if you ever have any problems with hackers?

    My last blog (wordpress) was hacked and I ended up losing months of hard work due to
    no backup. Do you have any solutions to stop hackers?

  5. Guadalupe表示:

    Hi there, just became aware of your blog through Google, and found that it’s really informative.
    I’m going to watch out for brussels. I’ll appreciate if you continue this in future.
    Numerous people will be benefited from your writing.
    Cheers!

  6. I’m not sure why but this site is loading very slow for me.
    Is anyone else having this issue or is it a issue
    on my end? I’ll check back later on and see if the problem still exists.

  7. relax gaming表示:

    Excellent web site you’ve got here.. It’s hard to find good quality writing like yours nowadays.
    I seriously appreciate individuals like you!
    Take care!!

  8. Hi, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam feedback?

    If so how do you protect against it, any plugin or anything you can suggest?
    I get so much lately it’s driving me crazy so any help is very
    much appreciated.

  9. I am sure this post has touched all the internet users, its really really nice paragraph on building
    up new weblog.

  10. docdro.id表示:

    What’s up, this weekend is nice in favor of me, for the reason that this occasion i am reading this wonderful educational piece of
    writing here at my home.

  11. Hello to all, it’s genuinely a good for me to pay a visit
    this web page, it consists of important Information.

  12. certainly like your web-site however you need to check the spelling on several of your posts.
    Many of them are rife with spelling issues and I find it very troublesome to inform the reality
    nevertheless I will certainly come again again.

  13. Mike表示:

    I’ll immediately grab your rss as I can’t find your email
    subscription hyperlink or e-newsletter service.
    Do you’ve any? Kindly allow me recognise in order that I may just subscribe.
    Thanks.

  14. I am extremely inspired together with your writing talents as well as with the format for your
    blog. Is that this a paid theme or did you customize it yourself?
    Either way keep up the nice high quality writing, it’s uncommon to see a nice weblog like this one these days..

  15. It’s remarkable to pay a quick visit this website and reading the views of all mates on the topic of this
    post, while I am also eager of getting knowledge.

  16. daftar togel表示:

    I used to be able to find good info from your blog articles.

  17. vimeo.com表示:

    When some one searches for his required thing, so he/she wishes to be available that in detail,
    thus that thing is maintained over here.

  18. tourism表示:

    I’m not sure where you are getting your information,
    but great topic. I needs to spend some time learning more or understanding more.
    Thanks for excellent information I was looking for this info for
    my mission.

  19. Heya are using WordPress for your blog platform?

    I’m new to the blog world but I’m trying to
    get started and create my own. Do you need any coding knowledge
    to make your own blog? Any help would be really appreciated!

  20. This is a very good tip especially to those new to the blogosphere.

    Brief but very accurate information… Thank you for sharing this one.
    A must read article!

  21. It’s going to be end of mine day, except before end I am reading this fantastic post to increase my know-how.

  22. I am sure this post has touched all the internet viewers, its really really fastidious piece of writing on building up new weblog.

  23. I used to be recommended this website via my cousin. I’m now not positive whether
    this post is written via him as nobody else recognise such specified about my trouble.

    You are amazing! Thanks!

  24. It’s going to be ending of mine day, however before ending I am reading
    this enormous paragraph to improve my know-how.

  25. vimeo.com表示:

    Heya i’m for the primary time here. I found this board and I find It truly helpful & it helped me out a lot.
    I am hoping to present something back and help others such as you aided me.

  26. Heya this is kind of of off topic but I was
    wanting to know if blogs use WYSIWYG editors or if you have to manually code with
    HTML. I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with
    experience. Any help would be enormously appreciated!

  27. This excellent website definitely has all the info I
    wanted concerning this subject and didn’t know
    who to ask.

  28. First off I would like to say great blog! I had a quick question that I’d like
    to ask if you do not mind. I was curious to find out
    how you center yourself and clear your thoughts prior to writing.
    I have had trouble clearing my thoughts in getting my ideas out there.
    I do enjoy writing but it just seems like the first 10
    to 15 minutes are generally wasted simply just trying to figure out
    how to begin. Any recommendations or hints? Many thanks!

  29. You could certainly see your expertise in the article you write.

    The sector hopes for more passionate writers like you who aren’t afraid to mention how they believe.
    Always go after your heart.

  30. Useful info. Lucky me I found your site unintentionally, and I’m stunned why this accident did not came about earlier!
    I bookmarked it.

發佈留言

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