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,232,663 Responses

  1. After I originally commented I appear to have clicked the -Notify me
    when new comments are added- checkbox and now every time a comment is added I receive 4 emails
    with the exact same comment. Perhaps there is an easy
    method you can remove me from that service? Thank you!

  2. I like the helpful info you provide for your articles.
    I will bookmark your blog and take a look at again here frequently.

    I am fairly certain I’ll learn a lot of new stuff right
    here! Best of luck for the following!

  3. telegra.ph表示:

    I am really thankful to the owner of this web site who has shared
    this fantastic article at at this place.

  4. What i don’t understood is in fact how you’re now not actually much more well-preferred than you might be
    now. You’re very intelligent. You recognize thus considerably relating to this subject,
    made me in my opinion believe it from so many numerous angles.
    Its like women and men don’t seem to be involved except it’s something to do with Woman gaga!
    Your personal stuffs outstanding. Always handle it
    up!

  5. Amazing blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own website 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
    options out there that I’m totally overwhelmed ..
    Any ideas? Kudos!

  6. Darlene表示:

    My family all the time say that I am wasting my time here
    at web, but I know I am getting knowledge every day by reading
    thes fastidious articles.

  7. Geraldo表示:

    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 augment and even I achievement you access consistently
    rapidly.

  8. I have been exploring for a little bit for any high-quality articles or
    weblog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this web site.

    Studying this info So i am happy to exhibit that I have
    a very good uncanny feeling I found out exactly what I needed.
    I such a lot unquestionably will make certain to do not forget this website and provides it a glance regularly.

  9. vimeo.com表示:

    Every weekend i used to pay a quick visit this
    web site, because i wish for enjoyment, as this this web
    page conations truly good funny data too.

  10. Hello there! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get my blog
    to rank for some targeted keywords but I’m not seeing very good success.
    If you know of any please share. Appreciate it!

  11. pbase.com表示:

    Wow, awesome weblog format! How long have you been running a blog for?
    you made running a blog glance easy. The overall
    look of your website is magnificent, as well as the content!

  12. Appreciate this post. Will try it out.

  13. When I originally left a comment I appear to have
    clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is
    added I get four emails with the exact same comment. Is there a way you are able to remove me from that service?
    Appreciate it!

  14. Nice post. I was checking continuously this blog and I’m impressed!
    Very useful info particularly the last part 🙂 I care for such
    info much. I was looking for this particular info for a
    very long time. Thank you and best of luck.

  15. Houston表示:

    Thanks for sharing your thoughts on website. Regards

  16. It’s perfect time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I desire to suggest you some interesting things or advice.
    Maybe you could write next articles referring to this article.
    I want to read more things about it!

  17. Whats up this is somewhat 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 skills so I wanted to get advice from someone with experience.
    Any help would be greatly appreciated!

  18. hey there and thank you for your information –
    I’ve certainly picked up something new from right here.
    I did however expertise some technical issues using this site,
    since I experienced to reload the web site many times previous to I could get it to load
    properly. I had been wondering if your hosting is OK? Not that I’m complaining, but sluggish
    loading instances times will often affect your placement in google and can damage your high-quality score if
    ads and marketing with Adwords. Well 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.

  19. Hi there I am so excited I found your website, I really found you by error, while I was
    looking on Digg for something else, Nonetheless I am here now and would just like to say many thanks for a tremendous post and a all round thrilling blog (I also love the
    theme/design), I don’t have time to browse it all at
    the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read a great
    deal more, Please do keep up the great jo.

  20. Truly no matter if someone doesn’t be aware of after that
    its up to other people that they will help, so here it takes
    place.

  21. I read this piece of writing fully concerning the difference of newest and earlier
    technologies, it’s awesome article.

  22. Normally I do not read article on blogs, but I would like to say that this write-up very forced
    me to take a look at and do so! Your writing style has been amazed me.
    Thanks, quite great article.

  23. Hi there, this weekend is nice designed for me, for the reason that this time i am reading this impressive educational piece of writing here at my residence.

  24. Madonna表示:

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

  25. flowad.net表示:

    First of all I would like to say terrific 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 thoughts before writing. I’ve had a hard time clearing
    my thoughts in getting my ideas out. I do take pleasure in writing but it just seems like the first 10 to 15 minutes are usually lost simply just trying to figure out how to begin. Any ideas or
    hints? Many thanks!

  26. judi online表示:

    I relish, cause I discovered exactly what I used to be looking for.
    You have ended my four day long hunt! God Bless you man. Have a
    nice day. Bye

  27. Do you mind if I quote a few of your articles as long as I provide credit and sources back to your
    blog? My blog site is in the exact same area of interest as yours
    and my visitors would truly benefit from a lot of the information you present here.
    Please let me know if this alright with you. Thank you!

  28. Just want to say your article is as surprising. The clearness for your submit is
    just great and that i can suppose you are knowledgeable in this subject.
    Fine with your permission allow me to snatch your
    RSS feed to stay up to date with impending post. Thank you one million and please
    carry on the gratifying work.

  29. Valuable information. Fortunate me I found your website by
    chance, and I am shocked why this twist of fate didn’t came about earlier!
    I bookmarked it.

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

    Make sure you update this again soon.

發佈留言

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