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

877,401 Responses

  1. Pretty section of content. I just stumbled upon your blog and in accession capital
    to assert that I acquire in fact enjoyed account your blog posts.
    Anyway I will be subscribing to your feeds and
    even I achievement you access consistently
    rapidly.

  2. I know this site presents quality dependent articles and
    other stuff, is there any other site which offers these stuff in quality?

  3. Everything is very open with a precise explanation of
    the issues. It was definitely informative. Your site is extremely helpful.
    Thank you for sharing!

  4. I think the admin of this website is actually working hard
    in favor of his site, as here every material is quality based data.

  5. discuss表示:

    What’s up, all is going well here and ofcourse every one is sharing
    information, that’s truly good, keep up writing.

  6. This design is steller! You obviously know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great
    job. I really enjoyed what you had to say, and more than that, how
    you presented it. Too cool!

  7. Metalean Diet表示:

    What’s up to every , for the reason that I am really keen of reading
    this webpage’s post to be updated on a regular basis.
    It carries pleasant data.

  8. Great post! We will be linking to this great post on our website.
    Keep up the great writing.

  9. First off I want to say wonderful blog! I had a quick question in which I’d like
    to ask if you do not mind. I was interested to find out how you center
    yourself and clear your head prior to writing.
    I’ve had trouble clearing my mind in getting my ideas out.

    I truly do enjoy writing however it just seems like the first 10 to 15 minutes tend to be lost simply just trying to figure
    out how to begin. Any suggestions or tips? Many thanks!

  10. You made some clear points there. I looked on the internet for the subject and found most people will approve with your site.

  11. An interesting discussion is definitely worth comment. I do believe that you need
    to publish more on this issue, it may not be a taboo matter but generally folks don’t speak about these topics.
    To the next! Cheers!!

  12. I am in fact grateful to the owner of this website who has
    shared this impressive paragraph at at this place.

  13. It’s fantastic that you are getting thoughts from this article as well as from our argument
    made here.

  14. I am genuinely grateful to the holder of this site who has shared
    this enormous paragraph at at this place.

  15. Unquestionably imagine that that you said. Your favorite justification appeared to be on the
    internet the easiest thing to keep in mind of. I say to you, I definitely get annoyed
    at the same time as people think about concerns that they just do
    not recognize about. You controlled to hit the nail upon the highest as neatly as defined out
    the whole thing without having side-effects , other
    folks can take a signal. Will probably be again to get more.
    Thanks

  16. First off I would like to say fantastic blog! I had a quick
    question in which I’d like to ask if you don’t mind.
    I was curious to find out how you center yourself and clear your thoughts before writing.

    I’ve had difficulty clearing my thoughts in getting my
    ideas out there. I do enjoy writing but it just seems like the first 10
    to 15 minutes tend to be wasted just trying to figure out how to begin.
    Any recommendations or hints? Kudos!

  17. Hello! I could have sworn I?ve been to your blog before but after looking at many of the posts I realized it?s new to me.
    Nonetheless, I?m certainly delighted I discovered it and I?ll be book-marking
    it and checking back often!

  18. I think this is one of the most important information for me.
    And i am happy reading your article. However wanna commentary on few basic things, The site taste is great, the articles is really excellent :
    D. Just right process, cheers

  19. Hi, I think your blog could possibly be having web browser compatibility
    issues. Whenever I take a look at your website in Safari, it looks fine but when opening in Internet
    Explorer, it’s got some overlapping issues. I just wanted to give
    you a quick heads up! Aside from that, wonderful site!

  20. oq-ayiq.net表示:

    Its like you learn my mind! You appear to know so much about this, such as you wrote the e-book in it
    or something. I believe that you just can do with a few p.c.
    to force the message home a bit, however instead of that, this
    is great blog. A fantastic read. I’ll certainly be back.

  21. Hi just wanted to give you a brief heads up and let you
    know a few of the images aren’t loading correctly.
    I’m not sure why but I think its a linking issue. I’ve tried it in two
    different browsers and both show the same outcome.

  22. discuss表示:

    hello there and thank you for your info – I’ve certainly picked up something new
    from right here. I did however expertise several technical points using this web site, since I
    experienced to reload the web site a lot of times previous to I could get it to load correctly.
    I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish
    loading instances times will very frequently affect your placement
    in google and can damage your high-quality score
    if ads and marketing with Adwords. Anyway I am
    adding this RSS to my email and can look out for a lot more of your respective interesting content.
    Ensure that you update this again very soon.

  23. I believe this is among the so much vital information for me.
    And i am happy reading your article. But want to remark on few basic issues,
    The site taste is great, the articles is in reality excellent : D.
    Good task, cheers

  24. Howdy excellent blog! Does running a blog such as this require
    a great deal of work? I have absolutely no knowledge of computer programming however I had been hoping to start
    my own blog soon. Anyway, should you have any ideas or techniques for
    new blog owners please share. I know this is off topic however
    I just needed to ask. Thank you!

  25. When someone writes an paragraph he/she maintains the thought of a
    user in his/her mind that how a user can understand it.
    Therefore that’s why this piece of writing is amazing.
    Thanks!

  26. toplist1.com表示:

    What i do not realize is in fact how you are not actually a lot more neatly-appreciated than you might be right now.

    You’re very intelligent. You understand thus significantly
    in terms of this topic, produced me individually believe it from
    a lot of numerous angles. Its like women and men don’t seem
    to be fascinated unless it’s something to accomplish with Lady gaga!
    Your personal stuffs outstanding. At all times handle it up!

  27. This blog was… how do you say it? Relevant!! Finally I’ve found something that helped me.
    Thanks!

  28. I do believe all of the ideas you’ve introduced in your post.
    They are really convincing and can definitely work. Still, the posts are too short for novices.

    May just you please prolong them a little from subsequent time?
    Thank you for the post.

  29. Wow that was strange. I just wrote an very long comment
    but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over
    again. Anyway, just wanted to say fantastic blog!

  30. Hi there, yup this article is truly fastidious and I have learned lot of things from it concerning blogging.
    thanks.

發佈留言

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