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,231,340 Responses

  1. Wonderful beat ! I would like to apprentice whilst you amend your
    web site, how could i subscribe for a weblog web site?
    The account aided me a appropriate deal. I had been tiny bit acquainted of
    this your broadcast offered vivid transparent idea

  2. I’m really impressed along with your writing talents and also with the format for your blog.
    Is this a paid subject matter or did you customize it
    yourself? Either way stay up the excellent quality writing,
    it’s rare to see a nice weblog like this one nowadays..

  3. Links wirken hier wie Empfehlungen in der realen Welt.
    Je vertrauenswürdiger eine empfehlende bzw.

    verweisende Quelle ist, desto wertvoller ist der Backlink bzw.
    die Empfehlung. Weniger die Anzahl der Links fast (Linkpopularität).
    Hier spielt in erster Linie die Anzahl der verschiedenen verlinkenden Domains, die sogenannte Domainpopularität, eine Rolle.
    Die zweite Ebene ist die thematische Beziehungsebene. Übertragen auf die Semantik bzw.
    Graphentheorie sind Links die Kanten zwischen den Knoten bzw.
    Entitäten. Link stellen egal Beziehungen zwischen den Dokumenten und deren dort abgebildeten Themen dar.
    Owned- und teilweise auch Self-Placed-Links wie Branchenbücher sind in erster Linie für die Entitäten-Bildung zuständig, während Paid- und Earned Links Sinn für die Verbesserung der Autorität einer Domain machen. Wir
    bei Aufgesang orientieren uns hier an ein semantisches Verlinkungskonzept- Mehr
    dazu im Beitrag Semantisch Themen finden: Wie identifiziert man semantisch verwandte Keywords?

    Umsetzung: Hier übernimmt der/die SEO-Manager(in) wieder zuerst eine beratende Funktion. Gemäß
    dem ob man sich für einen aktiven skalierbaren Linkaufbau oder organischen Linkaufbau via Content-Marketing entscheidet.

  4. agen pg soft表示:

    This paragraph provides clear idea designed for the new viewers of blogging, that really how
    to do blogging.

  5. Hi! This is my 1st comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your blog posts.
    Can you suggest any other blogs/websites/forums that cover the
    same topics? Thanks a lot!

  6. Danelle表示:

    I blog often and I truly thank you for your information. This
    article has really peaked my interest. I’m going to take a note of your website and
    keep checking for new information about once a week.
    I opted in for your Feed as well.

  7. Hey there, I think your site might be having browser
    compatibility issues. When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you a quick heads up!

    Other then that, awesome blog!

  8. Tremendous issues here. I am very satisfied to peer your
    post. Thank you so much and I’m taking a look ahead to contact you.
    Will you kindly drop me a e-mail?

  9. I used to be able to find good advice from your blog articles.

  10. fanpage表示:

    Hey! Would you mind if I share your blog with my twitter group?
    There’s a lot of people that I think would really enjoy your content.
    Please let me know. Cheers

  11. Undeniably believe that which you said. Your favorite justification seemed to be
    on the web the simplest thing to be aware of. I say to
    you, I definitely get irked while people think about worries that they just do not
    know about. You managed to hit the nail upon the top and
    also defined out the whole thing without having side-effects ,
    people could take a signal. Will probably be back to get more.
    Thanks

  12. I constantly emailed this web site post page to all my contacts, since if like to read it after that
    my friends will too.

  13. It’s remarkable in support of me to have a website, which is beneficial for my experience.
    thanks admin

  14. I have been browsing online more than 3 hours today, yet I never found
    any interesting article like yours. It’s pretty worth enough for me.
    In my opinion, if all webmasters and bloggers made good content as you did, the internet
    will be much more useful than ever before.

  15. I like the valuable info you provide in your articles.

    I’ll bookmark your blog and check again here frequently. I am quite sure I will
    learn a lot of new stuff right here! Good luck for the next!

  16. Hurrah, that’s what I was searching for, what a data! present
    here at this weblog, thanks admin of this website.

  17. What a information of un-ambiguity and preserveness of precious know-how regarding unexpected feelings.

  18. I enjoy looking through an article that will make people think.
    Also, many thanks for allowing me to comment!

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

  20. Brady表示:

    What’s up Dear, are you actually visiting this web site on a
    regular basis, if so afterward you will definitely obtain pleasant know-how.

  21. I know this web site gives quality based articles or reviews and additional
    data, is there any other site which presents such information in quality?

  22. We stumbled over here by a different page and thought I should check things out.
    I like what I see so now i am following you.
    Look forward to checking out your web page for a second time.

  23. Excellent site you have here but I was wanting to know if
    you knew of any user discussion forums that cover
    the same topics talked about here? I’d really like to be a part of
    online community where I can get feedback from other experienced people that share
    the same interest. If you have any suggestions, please
    let me know. Thanks!

  24. zghncy.cn表示:

    Undeniably believe that which you stated. Your favorite reason seemed
    to be on the internet the simplest thing to be aware of.
    I say to you, I definitely get irked while people consider worries that they just don’t know about.
    You managed to hit the nail upon the top and also defined out the whole thing without having
    side-effects , people can take a signal. Will probably be back to get more.
    Thanks

  25. I am regular reader, how are you everybody? This paragraph posted at this
    site is actually fastidious.

  26. My partner and I stumbled over here different page and thought I might as well check things out.
    I like what I see so now i am following you. Look forward to finding out about your web page yet
    again.

  27. Very nice post. I certainly love this website.
    Continue the good work!

  28. Hmm it seems like your blog ate my first comment (it was extremely long) so I guess I’ll just sum
    it up what I wrote and say, I’m thoroughly enjoying your
    blog. I as well am an aspiring blog writer but I’m still new to the whole thing.
    Do you have any helpful hints for newbie blog writers?
    I’d certainly appreciate it.

  29. dingdong表示:

    Hi there to every one, it’s actually a nice for me to pay a quick visit this web site, it
    contains precious Information.

  30. Greetings! Very helpful advice within this post!
    It is the little changes that make the most important changes.

    Thanks for sharing!

發佈留言

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