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

  1. I’m gone to inform my little brother, that he should also pay a quick visit this weblog on regular basis
    to take updated from latest reports.

  2. I always was concerned in this topic and still am, thank you for
    posting.

  3. I just like the valuable information you provide to your articles.
    I will bookmark your weblog and check again here regularly.
    I’m somewhat certain I will learn lots of new stuff right right here!
    Good luck for the following!

  4. In den letzten Jahren entscheiden sich junge Yakuza jedoch immer häufiger gegen die
    Irezumi, um sich besser in die japanische Gesellschaft zu integrieren. Außerdem ist der Prozess des Tätowierens nach traditioneller Weise (tebori) sehr langwierig und schmerzhaft.

    Dies ermöglicht ihnen bessere Kontakte zu hochrangigen Politikern oder Firmenchefs knüpfen zu
    können. Die traditionellen japanischen Tattoos folgen einer ganz bestimmten Geometrie auf
    den Körpern. Gemeint ist damit nicht, dass die Motive achsensymmetrisch gespiegelt werden, sondern dass die “Ärmel-Länge” jeweils gleich
    sein sollte. Teilweise dauert es mehrere Jahre in denen die Tattoo-Träger jede Woche für mehrere Stunden zum Tattoo Studio gehen müssen, damit dort der Tattoo-Künstler
    sein Kunstwerk vollbringen kann. Hierfür gibt es 3 typische Längen: Bis zum Schultergelenk, bis oberhalb des Ellbogens oder bis oberhalb des Handgelenks.
    Selbes gilt auch für die Beine. Außerdem werden weder der Kopf, Hals, noch Hände oder
    Füße mit dem Tattoo bedeckt. Häufig findet man auch eine Tattoo-Lücke
    die sich vom Hals über die Brust und den Bauch bis
    zum Genitalbereich erstreckt. Wenn man sich einen geöffneten Kimono vorstellt, wäre
    dieser Bereich sichtbar und soll deshalb “sauber” bleiben.

  5. Jodie表示:

    What’s Going down i am new to this, I stumbled upon this I
    have discovered It absolutely useful and it has helped me out loads.
    I hope to contribute & help different users like its helped me.

    Great job.

  6. Hi! Do you know if they make any plugins to help with SEO?
    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. Appreciate it!

  7. Hello to all, the contents existing at this web page are genuinely awesome for people
    knowledge, well, keep up the good work fellows.

  8. 196.43.133.60表示:

    Superb site you have here but I was curious if you knew of any message boards that cover the same topics talked about in this article?
    I’d really like to be a part of online community
    where I can get feed-back from other experienced people that share the same interest.

    If you have any recommendations, please let me
    know. Thanks!

  9. SUBSCRIPTION表示:

    Wow, superb blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look
    of your website is great, let alone the content!

  10. Hi my friend! I want to say that this post
    is amazing, nice written and include approximately all important infos.
    I’d like to look more posts like this .

  11. Valuable info. Lucky me I discovered your website
    by accident, and I am surprised why this twist of fate didn’t
    took place in advance! I bookmarked it.

  12. What’s up, just wanted to mention, I liked this blog post.
    It was practical. Keep on posting!

  13. Amazing! Its actually amazing paragraph, I have got much clear idea regarding from this paragraph.

  14. Holley表示:

    Thanks a bunch for sharing this with all of us you really realize what you are speaking about!
    Bookmarked. Please also discuss with my website =).
    We may have a link trade agreement between us

  15. discuss表示:

    I was suggested this website by means of my cousin. I’m not positive
    whether or not this publish is written by way of him
    as no one else recognize such certain about my difficulty.
    You are incredible! Thanks!

  16. Thanks for the good writeup. It if truth be told used to be a entertainment account it.

    Glance advanced to far introduced agreeable from you!
    However, how could we keep in touch?

  17. Fantastic goods from you, man. I’ve consider your stuff previous to and you are just too
    excellent. I really like what you have bought here, certainly like
    what you’re stating and the best way during which you assert it.
    You’re making it entertaining and you continue to take care
    of to stay it wise. I can’t wait to learn much more from you.

    This is actually a great site.

  18. What’s up mates, its fantastic paragraph concerning teachingand
    fully explained, keep it up all the time.

  19. Hello, yup this article is truly fastidious and I
    have learned lot of things from it regarding blogging. thanks.

  20. My spouse and I stumbled over here different
    web page and thought I should check things
    out. I like what I see so i am just following you. Look forward to looking over your
    web page again.

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

  22. orbit.o0o0.jp表示:

    Very energetic post, I enjoyed that bit. Will there be a part 2?

  23. Ahaa, its good conversation regarding this piece of writing here at this webpage,
    I have read all that, so at this time me also
    commenting here.

  24. Someone essentially help to make severely posts I might state.
    This is the very first time I frequented your web page and up to now?
    I surprised with the research you made to create this actual publish incredible.
    Excellent job!

  25. Hi, this weekend is good in support of me, as this time i am reading this impressive educational article here at my house.

  26. I pay a visit each day some web pages and blogs to read articles or reviews, but this web site offers
    quality based articles.

  27. Hi there just wanted to give you a quick heads up.

    The text in your article seem to be running off the
    screen in Ie. I’m not sure if this is a format issue or something
    to do with web browser compatibility but I thought I’d post to let you
    know. The layout look great though! Hope you get the problem fixed soon. Many thanks

  28. I do trust all the concepts you have introduced for your post.
    They are very convincing and will definitely work. Nonetheless, the posts are too quick for newbies.

    Could you please lengthen them a little from next time?
    Thanks for the post.

  29. I am really happy to read this website posts which includes plenty of valuable facts, thanks
    for providing these data.

發佈留言

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