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

886,840 Responses

  1. Thanks for your marvelous posting! I certainly enjoyed reading it, you happen to be a great author.
    I will make certain to bookmark your blog and will come back
    very soon. I want to encourage that you continue your great work, have a nice morning!

  2. Do you have a spam problem on this site; I
    also am a blogger, and I was wondering your situation; many of us have developed some nice methods and we are looking to exchange techniques with other folks, be sure to shoot me an e-mail if interested.

  3. Fine way of explaining, and pleasant post to take information regarding my presentation topic, which i am going
    to convey in institution of higher education.

  4. I don’t know if it’s just me or if perhaps everybody else encountering issues
    with your website. It appears as though some of the text
    in your posts are running off the screen. Can someone else please comment and
    let me know if this is happening to them as well? This might be
    a problem with my browser because I’ve had this happen before.
    Many thanks

  5. Hi there everybody, here every person is sharing
    such know-how, so it’s pleasant to read this webpage, and I used to pay a quick
    visit this website daily.

  6. Lashonda表示:

    I’m amazed, I must say. Rarely do I come across a blog
    that’s both educative and entertaining, and without a doubt, you have hit the nail on the head.
    The problem is something that too few people are speaking intelligently about.

    I am very happy I came across this in my hunt for something regarding this.

  7. Outstanding story there. What occurred after?
    Thanks!

  8. Your style is very unique in comparison to other people I’ve read stuff from.

    Many thanks for posting when you’ve got the opportunity, Guess I will just book mark this page.

  9. Hi to all, how is all, I think every one is getting more from this web site, and your views
    are nice in support of new viewers.

  10. Its not my first time to pay a visit this site,
    i am visiting this web page dailly and get good data from here everyday.

  11. Hello, i think that i saw you visited my site so i came to “return the favor”.I am trying
    to find things to improve my site!I suppose its ok to use some of your ideas!!

  12. It’s amazing to pay a visit this web page and reading the views of all
    mates regarding this paragraph, while I am also zealous
    of getting knowledge.

  13. I do not even know how I ended up here, but I thought this
    post was good. I don’t know who you are but definitely you’re going to a famous
    blogger if you aren’t already 😉 Cheers!

  14. George表示:

    Hmm it looks like your site ate my first comment (it was extremely
    long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying
    your blog. I too am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any suggestions for inexperienced blog writers?
    I’d really appreciate it.

  15. It’s very trouble-free to find out any matter on net as compared to books, as I found this piece of writing at
    this web page.

  16. Roxie表示:

    This is very interesting, You’re a very skilled blogger. I have
    joined your rss feed and look forward to seeking more of your magnificent post.
    Also, I’ve shared your web site in my social networks!

  17. Lilliana表示:

    I don’t even know how I ended up here, but I thought this post was
    good. I do not know who you are but definitely you’re going to a famous blogger if you aren’t already 😉 Cheers!

  18. Ida表示:

    Thank you for the auspicious writeup. It actually used to be a amusement account
    it. Look complex to far brought agreeable from you! By
    the way, how can we communicate?

  19. Hi i am kavin, its my first time to commenting anyplace, when i read
    this post i thought i could also create comment due to
    this good article.

  20. Wow, this article is fastidious, my younger
    sister is analyzing these things, so I am going to tell her.

  21. It’s very simple to find out any topic on web as compared to
    books, as I found this piece of writing at this web site.

  22. Brandy表示:

    For the reason that the admin of this site is working, no doubt very
    shortly it will be famous, due to its quality contents.

  23. Wow, this piece of writing is nice, my sister is analyzing these kinds of things, so I am going to convey her.

  24. Hanna表示:

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

  25. Everything is very open with a precise description of the issues.
    It was truly informative. Your site is extremely helpful.
    Thank you for sharing!

  26. Howdy I am so grateful I found your website, I really found you by accident,
    while I was searching on Bing for something else, Anyhow I am here now and would just like to say thanks
    for a fantastic post and a all round entertaining blog (I also love
    the theme/design), I don’t have time to browse it all at
    the moment but I have book-marked it and also added in your RSS feeds,
    so when I have time I will be back to read more, Please do keep up the great jo.

  27. It’s appropriate time to make some plans for the longer term and it’s time to be happy.
    I’ve learn this put up and if I could I desire
    to recommend you few attention-grabbing things or
    tips. Perhaps you can write subsequent articles regarding this article.
    I desire to read more things about it!

  28. always i used to read smaller posts which as well clear their motive, and that is also happening with this paragraph which I am
    reading here.

  29. Great goods from you, man. I have understand your stuff previous to and you’re just extremely excellent.
    I actually like what you have acquired here, certainly like what you are stating
    and the way in which you say it. You make it entertaining and you still care for to keep it smart.
    I cant wait to read much more from you. This is actually a wonderful web
    site.

  30. Carmine表示:

    Hello There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more of your useful information. Thanks for the post.
    I will certainly comeback.

發佈留言

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