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,253,168 Responses

  1. excellent publish, very informative. I’m wondering why the other
    experts of this sector do not understand this. You should proceed your writing.
    I am sure, you’ve a huge readers’ base already!

  2. Today, I went to the beach with my kids.
    I found a sea shell and gave it to my 4 year old daughter and
    said “You can hear the ocean if you put this to your ear.” She placed the shell
    to her ear and screamed. There was a hermit crab inside and it pinched
    her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

  3. Somebody necessarily help to make critically posts I’d state.
    This is the first time I frequented your website page
    and to this point? I amazed with the research you made to create this particular post amazing.

    Fantastic process!

  4. 80.82.64.206表示:

    I all the time used to study article in news papers but now as
    I am a user of web therefore from now I am using net for articles, thanks
    to web.

  5. QE Keto Boost表示:

    I’m not sure why but this site is loading extremely slow for me.

    Is anyone else having this issue or is it a problem on my end?

    I’ll check back later on and see if the problem still exists.

  6. You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never
    understand. It seems too complicated and very broad for me.
    I am looking forward for your next post, I will try to
    get the hang of it!

  7. Good write-up. I absolutely appreciate this site. Continue the good work!

  8. discuss表示:

    Wonderful blog! Do you have any hints for aspiring writers?
    I’m hoping to start my own site soon but I’m a little lost on everything.
    Would you recommend starting with a free platform like WordPress
    or go for a paid option? There are so many choices out there that I’m completely
    overwhelmed .. Any recommendations? Thanks a lot!

  9. Good web site you have here.. It’s difficult to find good quality
    writing like yours these days. I really appreciate individuals like you!

    Take care!!

  10. I got this web site from my buddy who informed me concerning this website and at
    the moment this time I am browsing this site and reading very informative
    posts here.

  11. madu samawa表示:

    At this time it looks like Drupal is the best blogging
    platform out there right now. (from what I’ve
    read) Is that what you’re using on your blog?

  12. I think that what you said made a lot of sense.

    However, think about this, what if you were to write a killer post title?
    I ain’t suggesting your content is not solid., however suppose you added something to maybe grab folk’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is a
    little plain. You ought to peek at Yahoo’s front page and see how they write news headlines to get viewers to open the links.

    You might try adding a video or a picture or two to grab readers excited about everything’ve got to
    say. Just my opinion, it could make your website a little bit more interesting.

  13. If you would like to get a great deal from this piece
    of writing then you have to apply such strategies to your
    won website.

  14. Its like you read my mind! You appear to know so much about
    this, like you wrote the book in it or something. I think that you
    can do with some pics to drive the message home
    a bit, but instead of that, this is excellent blog.

    An excellent read. I will certainly be back.

  15. This is very interesting, You are a very
    skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post.

    Also, I’ve shared your web site in my social networks!

  16. I think the admin of this web page is genuinely working hard in support of his web site, as here every stuff is quality based material.

  17. Katherin表示:

    What’s up i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also
    create comment due to this brilliant paragraph.

  18. controlc.com表示:

    Inspiring story there. What happened after? Thanks!

  19. Everything is very open with a precise description of the issues.
    It was really informative. Your site is useful.
    Thank you for sharing!

  20. Saved as a favorite, I really like your site!

  21. Ein Eigenbeleg wird vom Finanzamt nur als Notlösung
    anerkannt. Es herrscht zwar keine Begrenzung für die Anzahl der Ersatzbelege,
    die eingereicht werden dürfen, jedoch sollten Sie damit sparsam umgehen. Je mehr Eigenbelege ausgestellt werden, desto
    kritischer fragt das Finanzamt nach und desto höher ist die Möglichkeit, dass ein Beleg und folglich die dazugehörige Buchung nicht akzeptiert
    wird. Es herrscht zudem hilfreich, wenn Sie weitere Unterlagen vorweisen können,
    mehr oder weniger Notwendigkeit und Richtigkeit des Eigenbeleges zu untermauern. Dies kann beispielsweise ein Messeticket sein, wenn Sie einen Eigenbeleg für die dortige Garderobengebühr ausgestellt haben. Es kann verschiedene Gründe geben,
    warum ein Eigenbeleg ausgestellt werden muss: ein Automat
    hat keinen Beleg ausgegeben, die Aktentasche, in der sich der Beleg befand, wurde entwendet, der Beleg wurde versehentlich entsorgt oder es wurde vergessen nach der Ausstellung
    eines Beleges zu fragen. Wenn Sie bargeldlos gezahlt haben, können Sie dem Ersatzbeleg außerdem den entsprechenden Kontoauszug beifügen. Beleg ausstellen zu lassen. Weisen Sie
    Ihre Mitarbeiter darauf hin, stets nach einem Beleg
    zu fragen. Bewahren Sie Ihre Belege unterwegs untrennbar gut verschließbaren Behältnis wie einer Aktentasche auf.
    Denken Sie daran, sich in Restaurants etc.
    Digitalisieren Sie Ihre Belege. Mit lexoffice vermeiden Sie es, überhaupt
    einen Ersatzbeleg ausstellen zu müssen. Scannen Sie Ihre Belege einfach ein. Dank intelligenter
    Texterkennung (OCR) können Sie die wichtigsten Daten direkt aus dem Beleg übernehmen. Zahlung direkt dabei zugehörigen Beleg,
    sodass Sie sich das mühsame Abgleichen von Kontoauszügen und
    Rechnungen zukünftig verzichtbar. Dank der praktischen Scan-Funktion der lexoffice App für iOS und Android können Sie
    Ihre Belege direkt unterwegs einer der Kamera Ihres Tablets oder Smartphones aufnehmen und in lexoffice hochladen. So sind Ihre Belege sicher direkt nachdem Sie sie erhalten haben.

  22. 360nepal.net表示:

    Hi, just wanted to say, I loved this blog post. It was practical.

    Keep on posting!

  23. Everyone loves what you guys are usually up too.
    Such clever work and reporting! Keep up the amazing works guys I’ve included you guys to my personal blogroll.

  24. bags表示:

    Hi there, i read your blog from time to time and i own a similar one and i was just wondering if you get a lot of spam feedback?
    If so how do you stop it, any plugin or anything you can suggest?
    I get so much lately it’s driving me insane so any support is very
    much appreciated.

  25. ask.fm表示:

    Yes! Finally something about ask.fm.

  26. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is important and all. However think about if you
    added some great photos or video clips to give your posts more, “pop”!
    Your content is excellent but with pics and clips, this site could definitely be one of the very best in its niche.
    Awesome blog!

  27. What i don’t realize is in reality how you’re not
    actually a lot more well-preferred than you may be now.
    You’re so intelligent. You realize thus significantly in terms of this topic,
    produced me for my part believe it from a lot of various angles.
    Its like women and men are not interested unless it’s
    something to do with Woman gaga! Your individual stuffs outstanding.

    All the time handle it up!

  28. It is the best time to make some plans for the future and it is time to be happy.
    I have read this post and if I could I wish to suggest you some interesting
    things or tips. Perhaps you could write next articles referring
    to this article. I desire to read more things about it!

  29. I loved as much as you will receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get bought an shakiness over that you wish be delivering the following.

    unwell unquestionably come more formerly again as exactly the same nearly a lot often inside case you shield this hike.

發佈留言

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