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

633,328 Responses

  1. We’re a group of volunteers and opening a new scheme
    in our community. Your website provided us with valuable info to work on. You have done an impressive job and our entire community will be thankful to
    you.

  2. hello!,I love your writing so much! percentage we keep in touch more about your article on AOL?
    I require an expert on this area to unravel my problem.
    Maybe that is you! Taking a look forward to peer you.

  3. I every time used to study paragraph in news papers but now as I am a user of internet therefore from now I am
    using net for articles or reviews, thanks
    to web.

  4. I’m not sure exactly why but this site is loading incredibly slow for
    me. Is anyone else having this problem or is it a issue on my end?
    I’ll check back later on and see if the problem still exists.

  5. Aw, this was an extremely nice post. Finding the
    time and actual effort to generate a great article… but
    what can I say… I put things off a lot and never manage to get nearly anything done.

  6. I’m impressed, I must say. Seldom do I encounter a
    blog that’s both educative and entertaining, and
    let me tell you, you’ve hit the nail on the head.
    The issue is something not enough men and women are speaking intelligently about.
    I am very happy that I came across this during my search for something relating to this.

  7. Just desire to say your article is as surprising.
    The clarity to your submit is just spectacular and that i could suppose you are an expert on this subject.
    Well with your permission let me to grasp your feed to stay updated with approaching post.
    Thanks a million and please continue the enjoyable work.

  8. This website certainly has all of the information and facts I wanted concerning
    this subject and didn’t know who to ask.

  9. It’s amazing in favor of me to have a web page, which is beneficial
    designed for my know-how. thanks admin

  10. discuss表示:

    Ahaa, its good conversation regarding this
    paragraph here at this web site, I have read all that, so at this time me also commenting at this place.

  11. Great site you’ve got here.. It’s difficult to find excellent writing like yours nowadays.
    I truly appreciate people like you! Take care!!

  12. Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just too magnificent.
    I really like what you have acquired here, certainly like what you are stating and the way in which
    you say it. You make it enjoyable and you still take care of to keep it wise.
    I can’t wait to read far more from you. This is actually a great website.

  13. Hello! Do you know if they make any plugins to assist with Search Engine Optimization? I’m trying to get my
    blog to rank for some targeted keywords but I’m not seeing very good results.

    If you know of any please share. Thanks!

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

  15. Hey there! This is kind of off topic but I need some guidance from an established blog.
    Is it hard to set up your own blog? I’m not very techincal but
    I can figure things out pretty quick. I’m thinking about setting
    up my own but I’m not sure where to start.
    Do you have any ideas or suggestions? Cheers

  16. I like the valuable information you provide in your articles.
    I will bookmark your weblog and check again here
    frequently. I am quite certain I’ll learn many new stuff right here!
    Good luck for the next!

  17. My brother recommended I may like this website. He used to be entirely right.
    This publish truly made my day. You cann’t believe just how
    a lot time I had spent for this information! Thank you!

  18. Hey there! I realize this is sort of off-topic but I had to ask.
    Does managing a well-established website such as yours require a massive amount work?

    I’m completely new to writing a blog however I do write in my journal every day.
    I’d like to start a blog so I can easily share my personal experience
    and feelings online. Please let me know if you have any kind of suggestions or tips for brand
    new aspiring bloggers. Thankyou!

  19. Please let me know if you’re looking for a writer for your weblog.
    You have some really great posts and I believe I
    would be a good asset. If you ever want to take some of
    the load off, I’d really like to write some material for your blog in exchange for a
    link back to mine. Please send me an e-mail if interested.
    Regards!

  20. Hi! I’ve been reading your blog for a long time now and
    finally got the bravery to go ahead and give you a shout out from Lubbock Tx!
    Just wanted to say keep up the excellent job!

  21. Sarah表示:

    Hello, after reading this awesome article i am also happy to share
    my knowledge here with mates.

  22. I’m amazed, I must say. Rarely do I encounter a blog that’s both equally educative and entertaining,
    and without a doubt, you have hit the nail on the head.

    The problem is something that not enough men and women are speaking intelligently about.
    Now i’m very happy I found this during my hunt for something concerning this.

  23. I’m really enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more enjoyable for
    me to come here and visit more often. Did you hire out a developer
    to create your theme? Exceptional work!

  24. Excellent article. I certainly love this site. Thanks!

  25. You can definitely see your expertise within the article
    you write. The arena hopes for even more passionate writers such as you who are
    not afraid to say how they believe. At all
    times go after your heart.

  26. My coder is trying to persuade me to move to .net
    from PHP. I have always disliked the idea because of the
    expenses. But he’s tryiong none the less.
    I’ve been using WordPress on a number of websites for about a year and am nervous about switching to another platform.
    I have heard very good things about blogengine.net. Is there a way I can transfer all
    my wordpress content into it? Any help would be greatly appreciated!

  27. Lesli表示:

    Great delivery. Outstanding arguments. Keep up the good spirit.

  28. This paragraph is actually a pleasant one it helps new web users, who are
    wishing in favor of blogging.

  29. Hey there! I could have sworn I’ve been to this website before but after reading
    through some of the post I realized it’s new to me. Anyhow, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!

  30. It’s an amazing piece of writing in support of all the internet
    visitors; they will get benefit from it I am sure.

發佈留言

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