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

910,807 Responses

  1. It’s difficult to find experienced people for this subject, but
    you sound like you know what you’re talking about!

    Thanks

  2. Hi it’s me, I am also visiting this site on a regular basis,
    this website is actually pleasant and the users are in fact sharing pleasant thoughts.

  3. Thank you for the good writeup. It if truth be told was once a amusement account it.
    Look complicated to far added agreeable from you! By the way, how could
    we be in contact?

  4. peatix.com表示:

    Hi there 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 internet browsers and both show the
    same outcome.

  5. Hello there! I know this is kind of off topic but I was wondering which blog platform are you
    using for this website? I’m getting tired of WordPress because I’ve had problems with hackers and
    I’m looking at alternatives for another platform.
    I would be great if you could point me in the direction of a good platform.

  6. Dewayne表示:

    Hey there! I know this is kinda off topic but I was wondering which blog platform are you
    using for this website? I’m getting fed up of WordPress because I’ve had issues with hackers and I’m looking at options for another platform.
    I would be great if you could point me in the direction of a good platform.

  7. We absolutely love your blog and find most of your post’s to be exactly I’m looking for.
    Do you offer guest writers to write content for yourself?

    I wouldn’t mind composing a post or elaborating on most of the
    subjects you write regarding here. Again, awesome blog!

  8. Ready Keto表示:

    I like this post, enjoyed this one thanks for putting up.

  9. bdproshno.com表示:

    Hi there to every single one, it’s in fact a fastidious
    for me to pay a quick visit this site, it contains precious Information.

  10. It’s great that you are getting thoughts from this article as well as from our dialogue
    made at this time.

  11. Blythe表示:

    Awesome things here. I am very glad to peer your article.
    Thanks a lot and I’m looking forward to touch you.
    Will you please drop me a mail?

  12. Fine way of describing, and pleasant article to take facts regarding my presentation focus, which i am going to present in school.

  13. I am extremely impressed with your writing skills as neatly as with the structure on your blog.
    Is that this a paid subject or did you customize it
    yourself? Either way keep up the excellent high quality writing,
    it is rare to see a nice blog like this one these days..

  14. Have you ever considered creating an ebook
    or guest authoring on other websites? I have a blog based upon on the same topics you discuss and would
    love to have you share some stories/information. I know my viewers would enjoy your work.
    If you are even remotely interested, feel free to shoot me an email.

  15. Howdy just wanted to give you a quick 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.

  16. discuss表示:

    You could certainly see your skills within the article you write.

    The arena hopes for even more passionate writers like you who aren’t afraid to say how they believe.
    At all times go after your heart.

  17. For most up-to-date news you have to go to see world-wide-web and on world-wide-web I
    found this site as a most excellent web page for newest updates.

  18. Wow that was unusual. I just wrote an really long comment but after I clicked
    submit my comment didn’t show up. Grrrr… well I’m not writing
    all that over again. Anyway, just wanted to say excellent blog!

  19. website表示:

    I’ll right away gasp your rss as I can not to find your email subscription link or e-newsletter service.

    Do you’ve any? Please permit me recognize so that I may subscribe.

    Thanks.
    website

  20. I could not refrain from commenting. Very well written!

  21. Raymon表示:

    Truly when someone doesn’t understand then its up to other visitors that they will
    assist, so here it occurs.

  22. Alpha Gro表示:

    That is a great tip particularly to those fresh to the blogosphere.

    Brief but very accurate info? Thanks for sharing this one.
    A must read post!

  23. You really make it seem so easy with your presentation but
    I find this matter to be really one thing that I feel I’d
    never understand. It sort of feels too complex and extremely extensive for me.

    I am having a look forward for your next put up, I will try to get the dangle of it!

  24. My partner and I absolutely love your blog and find the majority of your post’s to be
    just what I’m looking for. Would you offer guest writers to write content in your case?
    I wouldn’t mind creating a post or elaborating on a number of the subjects you write
    regarding here. Again, awesome web site!

  25. wert-tools.ru表示:

    It’s a shame you don’t have a donate button! I’d certainly donate
    to this brilliant blog! I guess for now i’ll settle for
    book-marking and adding your RSS feed to my Google account.

    I look forward to fresh updates and will share
    this site with my Facebook group. Talk soon!

  26. This design is incredible! 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!

  27. Hi there! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any suggestions?

  28. Great post. I was checking continuously this weblog and I’m inspired!
    Very useful info specially the final part 🙂 I maintain such info a lot.
    I used to be looking for this particular information for a very long time.
    Thanks and best of luck.

  29. Joie表示:

    Its such as you learn my mind! You appear to understand a lot about this, such as you wrote the e book in it or something.
    I believe that you just could do with a few percent to power the message
    house a little bit, but other than that, that is wonderful blog.
    A great read. I will definitely be back.

  30. This is my first time pay a visit at here and i am genuinely impressed to read everthing at single place.

發佈留言

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