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,973 Responses

  1. Every weekend i used to visit this site, for the reason that i want
    enjoyment, as this this web page conations really fastidious funny data too.

  2. you are really a excellent webmaster. The web site loading pace is incredible.

    It kind of feels that you are doing any distinctive trick.

    Also, The contents are masterwork. you have performed a wonderful job
    in this subject!

  3. What’s up to every one, the contents existing at this website
    are actually amazing for people experience, well, keep up the good work fellows.

  4. Can you tell us more about this? I’d love to find out some additional information.

  5. Wow! This blog looks exactly like my old one!
    It’s on a completely different topic but it has pretty much the same layout and design. Excellent
    choice of colors!

  6. Everything is very open with a very clear clarification of the issues.
    It was definitely informative. Your website is extremely helpful.
    Many thanks for sharing!

  7. Simply desire to say your article is as astounding.
    The clarity in your post is simply cool and i can assume you’re
    an expert on this subject. Fine with your permission let me to grab your feed to keep up to date with forthcoming
    post. Thanks a million and please carry on the gratifying work.

  8. discuss表示:

    Actually when someone doesn’t be aware of then its up to other users that they will assist, so here it takes
    place.

  9. Excellent items from you, man. I’ve be aware your stuff prior
    to and you’re simply too fantastic. I really like what you
    have received right here, really like what you are saying and the way
    by which you say it. You’re making it entertaining and you still take care of to
    stay it sensible. I cant wait to learn much more from you.
    This is actually a wonderful site.

  10. Hi there! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in trading links or maybe guest writing
    a blog post or vice-versa? My site addresses a lot of the same topics as yours
    and I believe we could greatly benefit from each other.
    If you’re interested feel free to send me an email. I look forward to hearing from you!

    Terrific blog by the way!

  11. These are genuinely great ideas in on the topic of blogging.

    You have touched some nice points here. Any way keep up wrinting.

  12. It’s an amazing article in favor of all the internet viewers; they will get benefit from it I am sure.

  13. Thanks for some other informative site. Where else may I get that type of information written in such a
    perfect manner? I have a challenge that I’m simply now working
    on, and I’ve been on the look out for such info.

  14. Hi there to every body, it’s my first pay a visit of this web site; this
    web site includes remarkable and really good data in favor of readers.

  15. Good response in return of this issue with solid arguments and telling
    everything on the topic of that.

  16. Valuable information. Fortunate me I discovered your website by chance,
    and I’m shocked why this twist of fate didn’t came about in advance!

    I bookmarked it.

  17. I was suggested this blog via my cousin. I’m not sure whether this submit
    is written via him as nobody else understand such targeted approximately my
    trouble. You are wonderful! Thanks!

    Here is my blog slot367 pragmatic

  18. all the time i used to read smaller posts which as well clear their motive, and that is also happening with this article which I am
    reading now.

  19. I was wondering if you ever considered changing the layout of your blog?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

  20. Luisa表示:

    Right here is the perfect web site for anyone who
    wants to understand this topic. You realize a whole lot its almost tough to argue with you (not that I personally would want to…HaHa).
    You certainly put a brand new spin on a subject that’s been written about for many years.
    Great stuff, just wonderful!

  21. Hello there! This post could not be written any better!
    Reading this post reminds me of my previous room mate!
    He always kept talking about this. I will forward this article to him.

    Fairly certain he will have a good read.
    Thank you for sharing!

  22. It’s very easy to find out any matter on web as compared to books, as I found this post at this website.

  23. We are a group of volunteers and starting a new scheme
    in our community. Your web site provided us with valuable info to work on. You have done a
    formidable job and our entire community will
    be grateful to you.

  24. It’s remarkable to visit this web site and reading the views of all colleagues about this piece of writing, while I am also zealous of getting know-how.

  25. Pretty element of content. I just stumbled upon your blog
    and in accession capital to say that I acquire actually enjoyed account
    your weblog posts. Any way I will be subscribing on your feeds or even I success you get admission to constantly fast.

  26. What a data of un-ambiguity and preserveness of valuable familiarity regarding unpredicted emotions.

  27. Thankfulness to my father who told me on the topic of this webpage,
    this blog is actually amazing.

  28. I every time spent my half an hour to read this webpage’s
    articles or reviews all the time along with a mug of coffee.

  29. Quality content is the main to attract the people
    to pay a visit the web site, that’s what this web page is
    providing.

發佈留言

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