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,604,269 Responses

  1. Quality articles is the secret to invite the users to pay a
    quick visit the website, that’s what this website is
    providing.

  2. Just want to say your article is as surprising.

    The clarity for your put up is just nice and that i could suppose you are an expert on this
    subject. Well with your permission allow me to grasp your
    feed to stay up to date with impending post. Thank you one million and please keep up the gratifying work.

  3. I love what you guys tend to be up too. Such clever work and coverage!
    Keep up the fantastic works guys I’ve included you guys to my personal blogroll.

  4. When I originally commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I get several emails with
    the same comment. Is there any way you can remove me from that service?
    Cheers!

  5. Hal表示:

    Hi there, just became alert to your blog through Google, and found that it’s really
    informative. I am gonna watch out for brussels.
    I will appreciate if you continue this in future. Numerous people will be benefited from your writing.
    Cheers!

  6. Really no matter if someone doesn’t understand then its up to other visitors that they will
    help, so here it happens.

  7. parvona.net表示:

    Hi there to all, how is the whole thing, I think every one is getting more from this
    web site, and your views are pleasant in support of new people.

  8. This design is wicked! You obviously know how to keep a reader
    amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job.
    I really loved what you had to say, and more than that,
    how you presented it. Too cool!

  9. You really make it appear so easy with your presentation however
    I to find this topic to be actually something that I think I would never understand.
    It seems too complex and extremely vast for me.

    I’m looking forward in your next put up, I’ll attempt to get the hang of it!

  10. Way cool! Some extremely valid points! I appreciate you penning this post plus the rest
    of the site is really good.

  11. I’m not sure where you’re getting your information, but good topic.
    I needs to spend some time learning more or understanding more.
    Thanks for great info I was looking for this information for my mission.

  12. bdproshno.com表示:

    Thanks designed for sharing such a pleasant opinion, piece of
    writing is good, thats why i have read it fully

  13. Aisha表示:

    Hi, I do think this is a great website. I stumbledupon it 😉
    I may come back once again since i have saved as a favorite
    it. Money and freedom is the greatest way to change, may you be rich and continue to help others.

  14. I was suggested this website by my cousin. I’m not sure whether this post is written by him
    as nobody else know such detailed about my trouble. You’re wonderful!
    Thanks!

  15. Aw, this was an extremely nice post. Finding the time and actual effort to produce a superb article… but
    what can I say… I procrastinate a whole lot and
    never manage to get nearly anything done.

  16. Hi there, just became alert to your blog through Google, and
    found that it is truly informative. I’m going to
    watch out for brussels. I will appreciate if you continue this in future.
    A lot of people will be benefited from your writing. Cheers!

  17. Hey! Would you mind if I share your blog with my facebook
    group? There’s a lot of folks that I think would really enjoy your content.

    Please let me know. Cheers

  18. My brother suggested I might like this website. He was totally right.
    This post actually made my day. You cann’t imagine simply how
    much time I had spent for this information! Thanks!

  19. tattoo engel表示:

    Denn die meisten Motive für ein Paar-Tattoo sind auch noch nach dem (natürlich nicht gewünschten) Ende einer Beziehung
    schön und tragbar. Ihr sucht das passende Partner-Tattoo?
    Hier sind einige schöne Ideen für euer gemeinsames Tattoo.
    Du hast das passende Tattoo für dich und deinen Liebsten noch nicht gefunden? Vielleicht ist ja ein Motiv dabei, das für eure Liebe steht.
    Dein Browser kann dieses Video nicht abspielen. Derzeit total angesagt:
    Tattoos am Handgelenk. Klar, dass hier auch ein Pärchen-Tattoo sehr schön aussieht.
    Die Arme sind perfekt, um aller Welt (zumindest bei T-Shirt-Wetter) zu
    zeigen, dass man verliebt ist und sich zusammengehörig fühlt.
    Vor allem, weil man die beiden Tattoos einfach gegeneinander halten kann.
    Händchen halten ist dann noch viel schöner.
    Besonders schön sind Partner-Tattoos mit kleinen Motiven an den Fingern. Sie sind meist wie ein kleiner Verlobungs- oder Ehering – nur gestochen und
    für immer. Das Bein eignet sich besonders gut für diejenigen, die kein sichtbares
    Tattoo haben dürfen oder möchten. So kann man beispielsweise im Job einfach eine lange Hose anziehen und das Partner-Tattoo bleibt für die Öffentlichkeit
    versteckt. Es gibt kaum eine Stelle, die sich nicht eignen würde:
    Rücken, Nacken, Schultern, Fußgelenke, Hüfte oder sogar
    der Po bieten genügend Platz für eine schöne, kleine Liebeserklärung.
    Vielleicht war ja ein passendes Partner-Tattoo für euch dabei.

    Egal, was ihr euch gemeinsam stechen lassen wollt: Nehmt euch Zeit, bis ihr das passende Motiv für eure Beziehung gefunden habt.
    Es sollte auf jeden Fall beiden gefallen und zur Not auch noch nach einer Trennung deinen Geschmack treffen. Denn ein Tattoo bleibt mitunter länger
    als die Liebe und dann wäre es natürlich praktisch, wenn
    dir das Paar-Tattoo auch noch als Single-Tattoo gefällt.

  20. I always spent my half an hour to read this webpage’s
    articles daily along with a cup of coffee.

  21. Great delivery. Great arguments. Keep up the great work.

  22. Nice post. I learn something new and challenging on blogs I
    stumbleupon every day. It will always be useful to read through articles from other
    authors and use something from other sites.

  23. What i don’t understood is if truth be told how
    you’re now not actually much more smartly-appreciated than you might be
    right now. You’re very intelligent. You know therefore considerably in the case of this topic, made me in my opinion consider it from a lot of varied
    angles. Its like women and men aren’t interested except it’s
    one thing to accomplish with Girl gaga! Your individual stuffs outstanding.
    Always handle it up!

  24. I read this post fully concerning the difference of latest and previous technologies, it’s remarkable
    article.

  25. Very nice post. I just stumbled upon your blog and wished to say that I have truly enjoyed surfing around your blog posts.

    After all I’ll be subscribing to your feed and I hope you write again very soon!

  26. discuss表示:

    Good day! This post could not be written any better!

    Reading through this post reminds me of my old room mate!
    He always kept chatting about this. I will forward this post to him.
    Fairly certain he will have a good read. Thank you for sharing!

  27. We’re a group of volunteers and opening a new scheme in our community.
    Your site offered us with valuable info to work
    on. You’ve done an impressive job and our entire community will be thankful to you.

  28. Just wish to say your article is as amazing. The clearness in your post is
    just great and i could assume you’re an expert on this
    subject. Well with your permission let me to grab your feed to
    keep up to date with forthcoming post. Thanks a million and please keep up
    the rewarding work.

  29. Hi to all, as I am truly eager of reading this
    webpage’s post to be updated regularly. It consists of fastidious material.

發佈留言

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