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

841,743 Responses

  1. Can I simply say what a comfort to discover somebody that genuinely understands what they are talking about over
    the internet. You definitely know how to bring an issue to light and make it important.

    More people really need to check this out and understand this side of your story.
    I was surprised you aren’t more popular since you most certainly possess
    the gift.

  2. Hi everyone, it’s my first visit at this web page, and piece of writing is genuinely fruitful designed for
    me, keep up posting these types of articles.

  3. roomba 555表示:

    Wer im Urlaub oder im Büro nicht nur Fotos sehen möchte, die der Saugroboter während seiner Arbeit macht, sondern dieses Gerät auch mit allen seinen Funktionen steuern möchte,
    für den ist ein Saugroboter mit einer App-Fernsteuerung genau das
    richtige. Im Gegensatz zur klassischen Fernbedienung, über die heutzutage praktisch
    jeder Saugroboter verfügt, sind Geräte mit einer App-Fernsteuerung erst im aufwind.
    So sind sie z.B. Allerdings sind solche Geräte meist erheblich vielseitiger als ihre klassischen Konkurrenten ohne App-Fernsteuerung.
    Lage, den Wohnraum mit einer integrierten Kamera mit Bewegungssensor zu überwachen,
    sie können Mails mit Fotos an den Besitzer schicken und gezielt in Wohnräumen nachschauen,
    ob das Licht (oder z.B. Im Großen und Ganzen können Saugroboter auch völlig ohne Licht arbeiten. Zu
    beachten ist aber: Staubsaugerroboter mit Kamera-Navigation finden sich in abgedunkelten Räumen nicht länger zurecht,
    ihre Navigation versagt hier völlig. Dagegen funktioniert die Steuerung über virtuelle Wände bzw.
    Leuchttürme auch in völlig dunklen Räumen ausgezeichnet, da
    hier mit unsichtbaren Infrarotstrahlen gearbeitet wird.

  4. I am extremely inspired together with your writing talents and
    also with the layout in your weblog. Is this a paid subject or did you modify it your self?
    Anyway keep up the nice quality writing, it’s rare to see a
    great weblog like this one today..

  5. portal5表示:

    many thanks a great deal this website is definitely elegant in addition to casual

  6. Hey there, You’ve done a great job. I will definitely digg it and personally suggest to my friends.
    I am confident they will be benefited from this site.

  7. 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 wonderful info I was looking for this info for my mission.

  8. discuss表示:

    I pay a visit every day a few web sites and websites to read posts, but this webpage presents feature based
    content.

  9. Thank you for any other excellent post. The place else could anyone get that
    type of information in such a perfect approach of writing?

    I’ve a presentation subsequent week, and I’m at the search for such information.

  10. Penny表示:

    handy review. Moreover visit typically the site to know slot online pulsa.
    This kind of website has received recognition via slot deposit pulsa tanpa potongan gambling avid gamers in Indonesia.
    Presently there are generally lots of ideal promos if you happen to join at the moment.

  11. I have fun with, cause I discovered exactly what I used to be looking for.

    You’ve ended my 4 day lengthy hunt! God Bless you man. Have a great day.

    Bye

  12. taxi表示:

    We absolutely love your blog and find almost all of your post’s to be exactly what I’m looking
    for. can you offer guest writers to write content for
    yourself? I wouldn’t mind writing a post or elaborating on a lot of
    the subjects you write concerning here. Again, awesome website!

  13. Pretty! This was a really wonderful post. Thank you for providing
    these details.

  14. Does your website have a contact page? I’m having problems locating it but, I’d like to shoot you
    an email. I’ve got some recommendations for your blog you
    might be interested in hearing. Either way, great blog and I look forward to seeing it improve over time.

  15. My spouse and I stumbled over here by a different page and
    thought I may as well check things out. I like what I
    see so i am just following you. Look forward to looking into your web page for a second time.

  16. This is very fascinating, You’re a very skilled blogger.
    I’ve joined your rss feed and stay up for in the
    hunt for extra of your fantastic post. Additionally,
    I have shared your site in my social networks

  17. Oh my goodness! Impressive article dude! Many thanks, However I am going
    through troubles with your RSS. I don’t understand the reason why I cannot join it.

    Is there anyone else having the same RSS problems?

    Anyone who knows the answer will you kindly respond?
    Thanx!!

  18. Definitely imagine that that you stated. Your favourite reason appeared to be on the net the simplest factor to keep in mind of.

    I say to you, I certainly get annoyed even as folks think
    about worries that they just do not realize about.
    You controlled to hit the nail upon the top and defined
    out the whole thing without having side-effects , people can take a signal.
    Will probably be again to get more. Thank you

  19. I believe what you said was very logical.
    However, consider this, suppose you typed a catchier post title?
    I mean, I don’t want to tell you how to run your blog, however suppose you added a post title that grabbed a person’s attention? I mean JS 依照元素出現數量排序
    – 馬老師 雲端研究室 is a little boring.
    You should look at Yahoo’s home page and see how they write post titles to grab
    viewers interested. You might try adding a video or a related pic or two to grab readers interested about what you’ve
    written. Just my opinion, it might bring your posts a little bit
    more interesting.

  20. I know this if off topic but I’m looking into starting my
    own weblog and was wondering what all is required
    to get set up? I’m assuming having a blog like yours would
    cost a pretty penny? I’m not very internet savvy so I’m not 100% positive.
    Any tips or advice would be greatly appreciated. Appreciate it

  21. Hello friends, good article and good urging commented here, I am actually enjoying by these.

  22. Excellent goods from you, man. I have understand your stuff previous to and you’re
    just extremely wonderful. I actually like what you’ve 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 sensible. I cant wait to read much more from you.

    This is really a terrific web site.

  23. Hurrah! After all I got a website from where I be able to truly get valuable data concerning my study and knowledge.

  24. Hello there, I do believe your blog might be having browser compatibility problems.
    When I look at your site in Safari, it looks fine but when opening in IE, it
    has some overlapping issues. I just wanted to provide you with a quick heads up!

    Other than that, fantastic blog!

  25. Great blog you have got here.. It’s hard to find high-quality writing like yours these days.
    I seriously appreciate people like you! Take care!!

  26. Hello, i believe that i noticed you visited my website so
    i got here to go back the want?.I am attempting to find things to enhance my site!I suppose its ok
    to make use of some of your ideas!!

  27. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to more added agreeable from you! However,
    how can we communicate?

  28. I’m amazed, I have to admit. Seldom do I encounter a blog that’s equally educative and engaging,
    and let me tell you, you’ve hit the nail on the head. The problem is something
    that too few people are speaking intelligently about. I am very
    happy that I found this during my search for something regarding this.

  29. escort duzce表示:

    Your style is really unique in comparison to other folks I’ve read stuff from.
    I appreciate you for posting when you’ve got the opportunity, Guess I will
    just bookmark this page.

  30. Excellent website. A lot of helpful information here.
    I’m sending it to several pals ans additionally sharing in delicious.
    And naturally, thank you in your sweat!

發佈留言

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