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,233,358 Responses

  1. notes.io表示:

    Admiring the time and energy you put into your website and in depth
    information you provide. It’s good to come across a blog every once in a
    while that isn’t the same unwanted rehashed information.
    Great read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  2. Hey, I think your website might be having browser compatibility issues.
    When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, amazing blog!

  3. ask.fm表示:

    Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is valuable and all.
    However think of if you added some great visuals or
    video clips to give your posts more, “pop”! Your content is excellent but with pics and video clips, this website could undeniably be one of the very
    best in its niche. Excellent blog!

  4. I am genuinely thankful to the holder of this web page who has shared
    this enormous article at here.

  5. Very good blog post. I certainly love this site. Continue the good work!

  6. Excellent site you have here but I was wanting to know if you knew of any community forums
    that cover the same topics discussed here? I’d really like to be
    a part of group where I can get feed-back from other experienced individuals that share
    the same interest. If you have any recommendations, please let me know.
    Bless you!

  7. If you wish for to increase your familiarity just keep visiting this web page and
    be updated with the most recent gossip posted here.

  8. Hello there! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly? My blog
    looks weird when viewing from my iphone4. I’m trying to find a theme or plugin that might
    be able to fix this issue. If you have any suggestions, please share.
    Appreciate it!

  9. We’re a group of volunteers and starting a new scheme in our
    community. Your web site provided us with valuable information to work
    on. You’ve done a formidable job and our entire community will be thankful to you.

  10. Hi, I do think this is a great blog. I stumbledupon it 😉 I
    will return yet again since I book marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.

  11. frisuren表示:

    Solche damen sind mit besonderem charme gesegnet.
    Der stumpfe bob wird meistens glatt getragen, eignet sich aber auch gut für welliges oder dünnes, lockiges haar.
    Frisuren bob vorne lang hinten kurz ein langer, umgekehrter bob mit pony ist der perfekte haarschnitt für damen, die gerne auffallen.
    Das macht sich besonders im bob gut. Sie müssen nicht
    zwischen extremen hetzen. 06.08.2018 · vorherige
    next 1 von 60 weiter frisuren für frauen über 60
    bedeuten nicht langweilige kurzhaarschnitte oder veraltete kopftücher.
    08.04.2021 · der blunt bob, echt stumpfer bob, ist momentan mit das angesagtesten frisuren bei den promis.
    Chic short hair ideas … 06.08.2018 · vorherige next 1 von 60 weiter frisuren für frauen über
    60 bedeuten nicht langweilige kurzhaarschnitte oder veraltete kopftücher.

    Frisuren bob vorne lang hinten kurz ein langer, umgekehrter
    bob mit pony ist der perfekte haarschnitt für damen, die gerne auffallen. Sie müssen nicht zwischen extremen hetzen.
    08.04.2021 · der blunt bob, auf deutsch stumpfer bob, ist momentan mit die angesagtesten frisuren bei den promis.
    11.09.2021 · wenn wir welliges oder lockiges haar
    haben, ist es für reife frauen sehr wichtig, den schnitt definiert und sauber zu halten.

  12. Awesome! Its truly awesome post, I have got much clear idea concerning from
    this post.

  13. I have been browsing online greater than 3 hours as of late, yet I never discovered any attention-grabbing article like yours.

    It’s pretty price sufficient for me. In my opinion, if all site owners and
    bloggers made excellent content material as you did, the net will likely be much more useful than ever before.

  14. I all the time used to read article in news papers but now as
    I am a user of internet so from now I am using net
    for content, thanks to web.

  15. ask.fm表示:

    Just desire to say your article is as amazing. The clarity in your submit is just cool and that
    i can think you are an expert on this subject. Well with your permission let me to grab your
    RSS feed to keep updated with coming near near post. Thanks 1,000,000 and please continue the rewarding work.

  16. I’m no longer certain the place you are getting your information, however good topic.
    I needs to spend some time finding out much more or understanding more.
    Thanks for excellent information I was looking for this info
    for my mission.

  17. Thank you for the good writeup. It if truth be told was
    once a leisure account it. Look complex to more delivered agreeable from you!
    By the way, how could we be in contact?

  18. What’s up to every body, it’s my first visit of
    this web site; this web site includes awesome and actually fine
    stuff designed for readers.

  19. Article writing is also a fun, if you be acquainted with after that you can write or else it
    is complex to write.

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

  21. Greetings! Very useful advice in this particular article!

    It’s the little changes which will make the most significant changes.
    Thanks a lot for sharing!

  22. I do not know whether it’s just me or if everyone else experiencing problems with your blog.
    It looks like some of the written text on your posts are running
    off the screen. Can somebody else please provide feedback and let me know
    if this is happening to them as well? This might be a problem with my internet browser because I’ve
    had this happen before. Many thanks

  23. Thankfulness to my father who stated to me concerning
    this website, this web site is in fact amazing.

  24. toplist1.com表示:

    My brother recommended I might like this blog. He was entirely right.
    This post truly made my day. You cann’t imagine just
    how much time I had spent for this information! Thanks!

  25. I’m not sure exactly why but this blog is loading incredibly slow for me.
    Is anyone else having this issue or is it a problem on my end?

    I’ll check back later and see if the problem still exists.

  26. Link exchange is nothing else however it is just placing the
    other person’s webpage link on your page at
    suitable place and other person will also do similar in support of you.

  27. Link exchange is nothing else except it is simply placing
    the other person’s weblog link on your page at appropriate
    place and other person will also do similar for you.

  28. almstbah.net表示:

    This design is steller! You most certainly 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!) Wonderful
    job. I really enjoyed what you had to say, and more than that, how you presented it.

    Too cool!

  29. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment.
    Is there any way you can remove people from that service?
    Thank you!

  30. Hi! This is kind of off topic but I need some help from an established blog.
    Is it very hard to set up your own blog? I’m not very techincal but I can figure things
    out pretty quick. I’m thinking about making my own but I’m not
    sure where to begin. Do you have any ideas or suggestions?
    With thanks

發佈留言

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