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,228,056 Responses

  1. Fantastic goods from you, man. I have understand your stuff previous to and you are just too fantastic.
    I actually like what you’ve acquired here, certainly like what you’re stating and the way in which you say it.
    You make it enjoyable and you still care for to keep it sensible.
    I can’t wait to read much more from you. This is really a terrific website.

  2. Greetings from Idaho! I’m bored at work so I decided to check out your blog
    on my iphone during lunch break. I love the info you
    provide here and can’t wait to take a look when I get home.
    I’m surprised at how fast your blog loaded on my cell phone ..

    I’m not even using WIFI, just 3G .. Anyways, great site!

  3. You can certainly see your enthusiasm within the article you write.
    The world hopes for more passionate writers such as you who aren’t afraid to say how they believe.
    All the time follow your heart.

  4. I’m curious to find out what blog platform you have been utilizing?

    I’m having some minor security issues with my latest site and
    I would like to find something more safe. Do you have any recommendations?

  5. May I simply say what a relief to discover someone that truly
    knows what they’re discussing on the web. You actually know how to bring a problem
    to light and make it important. More people ought to look at this and understand this
    side of your story. I was surprised you aren’t more popular given that you
    definitely have the gift.

  6. Thanks for another informative website. The place else could I get that type of info written in such a perfect means?
    I have a challenge that I am simply now operating on, and I’ve been at the glance out for such
    information.

  7. What’s Happening i’m new to this, I stumbled upon this I’ve discovered
    It positively useful and it has aided me out loads. I’m
    hoping to give a contribution & help other users like its aided me.
    Great job.

  8. Ahaa, its fastidious dialogue on the topic of this article at this place at this weblog, I have
    read all that, so at this time me also commenting here.

  9. I’m amazed, I must say. Seldom do I encounter a blog that’s equally educative and entertaining,
    and without a doubt, you have hit the nail on the head. The issue is something
    not enough people are speaking intelligently about.
    I am very happy that I stumbled across this during my
    search for something regarding this.

  10. I am really pleased to glance at this blog posts which includes lots of useful
    information, thanks for providing these kinds of statistics.

  11. I was wondering if you ever thought of changing the structure of your site?
    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 1 or two images.

    Maybe you could space it out better?

  12. Hurrah, that’s what I was looking for, what a material!

    existing here at this weblog, thanks admin of this web site.

  13. I just could not depart your web site prior to suggesting that I
    actually enjoyed the usual information an individual provide in your guests?

    Is gonna be back often in order to check up on new posts

  14. Howdy just wanted to give you a brief heads up and let you know a few
    of the pictures aren’t loading properly. I’m not sure why
    but I think its a linking issue. I’ve tried it
    in two different browsers and both show the same results.

  15. Asking questions are in fact good thing if you are not understanding anything entirely,
    but this post presents good understanding even.

  16. Have you ever thought about writing an e-book or guest authoring on other sites?
    I have a blog based on the same topics you discuss and would
    love to have you share some stories/information. I
    know my viewers would value your work. If you are even remotely interested, feel free
    to shoot me an e mail.

  17. Great goods from you, man. I’ve consider your stuff prior to and
    you are just too wonderful. I actually like
    what you’ve received right here, certainly like what you are stating and
    the way in which through which you say it. You’re making it entertaining and you continue
    to take care of to stay it sensible. I can not wait to read far more
    from you. This is really a great website.

  18. Wonderful post! We are linking to this great content on our site.
    Keep up the good writing.

  19. Hi, I do believe this is a great site. I stumbledupon it
    😉 I’m going to come back yet again since i have book-marked it.
    Money and freedom is the greatest way to change, may you be rich and continue to
    guide others.

  20. Hello There. I found your weblog the usage of msn. That is an extremely smartly written article.
    I’ll make sure to bookmark it and return to learn extra of your useful information. Thanks
    for the post. I will certainly return.

  21. I every time emailed this webpage post page to all my associates, because if like to
    read it afterward my links will too.

  22. Heya! I just wanted to ask if you ever have any issues with hackers?

    My last blog (wordpress) was hacked and I ended up losing months of hard work
    due to no backup. Do you have any solutions to stop hackers?

  23. Woah! I’m really digging the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between usability and visual appeal.
    I must say you have done a great job with this.
    Additionally, the blog loads very quick for me on Firefox.
    Superb Blog!

  24. Fine way of describing, and nice piece of writing
    to take information about my presentation subject, which i am going to present in college.

  25. It’s really a great and useful piece of info.
    I am satisfied that you simply shared this helpful information with
    us. Please stay us up to date like this. Thanks for sharing.

  26. holyfenster表示:

    Das Material des Randprofils und das Polyisobutylen müssen den Randverbund gegenüber Füllgas,
    Umgebungsluft und Wasserdampf abdichten. Nach der Befüllung des Scheibenzwischenraums mit Gas wird diese Fuge zwischen dem um etwa 3
    mm eingerückten Abstandhalter und den überstehenden Glaskanten mit pastösem Polyurethan, Silikon oder
    speziellen Polysulfiden gefüllt. Bei Fassadenelementen, die an dieser
    Stelle dem UV-Licht ausgesetzt sind, wird meist schwarzes Silikon statt Butyl, Polyurethan oder Polysulfid verwendet, das allerdings deutlich gasdurchlässiger
    ist. Der Rahmen aus Abstandhalter-Profilen wird etwas kleiner gefertigt, als die
    Glasscheiben, auf die weise, dass nach dem Einkleben des Glashalters eine U-förmige Fuge zwischen den Glasrändern verbleibt.

    Hier kann etwa eine Weichmacherwanderung oder ein Kontakt mit schädlichen Ölen oder anderen Stoffen stattfinden. Problematisch sind Unverträglichkeiten zwischen den Dichtstoffen, die zur Eindichtung der Isolierglasscheibe im Fensterrahmen oder als Dichtung des Stoßes zwischen zwei Scheiben Verwendung finden, und den Dichtstoffen im
    Randverbund. Ebenso können auch schädliche Wechselwirkungen zwischen dem Kunstoffmaterial
    der Verglasungs-Klötze und dem Randverbund auftreten, insbesondere,
    wenn die Klötze Styrolverbindungen enthalten.

  27. You really make it appear really easy along with your presentation but I to find this matter to be actually
    something that I feel I would by no means understand.
    It seems too complicated and extremely broad for me.
    I’m looking ahead for your next put up, I’ll try to get the cling of it!

  28. Thanks for sharing your info. I truly appreciate your efforts and I am waiting for your next write
    ups thank you once again.

  29. Definitely consider that that you said. Your favourite
    justification seemed to be on the internet the easiest
    factor to be mindful of. I say to you, I certainly get irked even as other people think about issues that they
    plainly do not recognise about. You managed to hit the nail upon the highest and defined out the whole thing without having side effect
    , other people could take a signal. Will probably be
    back to get more. Thank you

  30. Excellent, what a weblog it is! This blog provides helpful data to us,
    keep it up.

發佈留言

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