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,249,411 Responses

  1. housefast表示:

    Right now it sounds like WordPress is the top blogging platform available right now.
    (from what I’ve read) Is that what you’re using on your blog?

    Here is my page :: housefast

  2. If some one wishes expert view about blogging and site-building afterward i advise him/her to go to
    see this webpage, Keep up the pleasant job.

  3. naked表示:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to
    make your point. You clearly know what youre talking about, why waste your intelligence on just
    posting videos to your weblog when you could be giving us something informative to read?

  4. Excellent site you have here but I was wondering if you knew of any discussion boards that cover the same topics
    talked about here? I’d really love to be a part of group where I can get opinions from other experienced people that share
    the same interest. If you have any recommendations, please let me know.
    Many thanks!

  5. you’re truly a excellent webmaster. The web site loading speed
    is incredible. It seems that you’re doing any unique trick.
    Moreover, The contents are masterpiece. you have
    done a excellent job on this subject!

  6. Hi my family member! I wish to say that this article is awesome, great written and include approximately
    all important infos. I would like to look extra posts like this .

  7. Good post. I am going through many of these issues as well..

  8. I read this paragraph completely regarding the comparison of most up-to-date and
    previous technologies, it’s awesome article.

  9. Hi there! 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. Thank you!

  10. As I web site possessor I believe the content matter here is rattling great , appreciate it for your hard
    work. You should keep it up forever! Good Luck.

  11. What’s up to every one, for the reason that I am in fact eager of reading this
    weblog’s post to be updated daily. It includes pleasant data.

  12. Great website. A lot of useful info here. I am sending it to some friends ans also
    sharing in delicious. And naturally, thanks in your sweat!

  13. important review. Furthermore visit many of our site to learn slot deposit pulsa tanpa potongan. This amazing site
    has received excellent reputation right from slot pulsa tanpa potongan wagering associates in Dalam negeri.
    At this time there are actually many eye-catching
    promos need to anyone previously join presently.

  14. 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 user friendliness and visual appeal. I must say that you’ve done
    a very good job with this. Additionally, the blog loads very
    fast for me on Firefox. Exceptional Blog!

  15. When I originally left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox
    and now each time a comment is added I receive 4 emails with the same
    comment. Is there a way you are able to remove me from that service?
    Appreciate it!

  16. Frank表示:

    Hello There. I found your blog using msn. This is an extremely well written article.
    I will be sure to bookmark it and come back to read
    more of your useful information. Thanks for the
    post. I’ll definitely comeback.

  17. ask.fm表示:

    I am genuinely pleased to read this website posts which carries lots of valuable information, thanks for providing these statistics.

  18. Thank you for the good writeup. It in fact was a amusement account it.
    Look advanced to far added agreeable from you! By the way, how could we communicate?

  19. We stumbled over here coming from a different web
    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 repeatedly.

  20. Great delivery. Sound arguments. Keep up the amazing spirit.

  21. discuss表示:

    Wow, that’s what I was exploring for, what a stuff!
    existing here at this blog, thanks admin of this
    site.

  22. You are a very smart individual!

  23. Hi there, I would like to subscribe for this web site to obtain most
    recent updates, so where can i do it please help.

  24. 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 have shared your web site in my social
    networks!

  25. 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 could do with some pics to drive the message home a little bit, but instead of that, this is great blog.
    A great read. I’ll definitely be back.

  26. Admiring the dedication you put into your blog and in depth information you provide.

    It’s awesome to come across a blog every once
    in a while that isn’t the same outdated rehashed material.
    Wonderful read! I’ve bookmarked your site and I’m adding your RSS feeds to my
    Google account.

  27. If you wish for to grow your experience simply keep visiting this web site and be updated with the most up-to-date information posted here.

  28. Hey are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and set up my
    own. Do you need any html coding expertise to make your own blog?
    Any help would be really appreciated!

  29. Piece of writing writing is also a excitement, if you know afterward you can write or else it is difficult to
    write.

  30. Zusätzlich bestimmt das Kabel die Reichweite – ist es nicht lang genug,
    muss der Staubsauger an einer anderen Steckdose angeschlossen werden. Beim Akkusauger treten diese Probleme des Saugens nicht auf.
    Er besteht aus einer verstellbaren Saugstange ohne einen sperrigen Korpus und Kabel.
    Treppen stellen jeden Bodensauger vor eine Herausforderung.
    In beiden Fällen bietet der Akku-Staubsauger mit leichtem tragbarem Korpus mehr Flexibilität und eine einfachere
    Handhabung. Bei kabelgebundenen Staubsaugern ist der Saugschlauch zumeist nicht sehr
    lang, sodass höher gelegene Stellen nicht oder nur schwer zu erreichen sind.

    Wie gut reinigen Akku-Staubsauger den zu saugenden Bereich?

    Die Saugleistung der Akku-Staubsauger wird von den kabelgebundenen Bodenstaubsaugern übertroffen. Akku-Staubsauger haben nicht nur Vorteile gegenüber herkömmlichen Bodenstaubsaugern,
    sondern bringen auch Nachteile mit sich, die hier gegenübergestellt werden. Kabelgebundene Staubsauger können beliebig lang auf beliebig starkem
    Saug-Modus herhalten müssen, während die Laufzeit des Akkusaugers erst
    recht bei hoher Saugstufe stark begrenzt ist.

    Viele Akku-Staubsauger halten im stärksten Saug-Modus nur wenige Minuten durch.
    Manche Modelle haben eine Laufzeit von nur 10 Minuten, was meist nicht ausreicht, um den gesamten Haushalt zu säubern.

發佈留言

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