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,229,566 Responses

  1. You’re so cool! I don’t believe I’ve read anything like
    that before. So good to discover another person with some original
    thoughts on this topic. Seriously.. many thanks for starting this up.
    This web site is something that is required on the internet,
    someone with a bit of originality!

  2. shorl.com表示:

    I am genuinely happy to glance at this blog posts which consists of plenty of helpful
    facts, thanks for providing such statistics.

  3. Hello! This is kind of off topic but I need some guidance from an established
    blog. Is it difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about making my own but I’m not sure where
    to begin. Do you have any ideas or suggestions?
    With thanks

  4. Does your website have a contact page? I’m having trouble
    locating it but, I’d like to shoot you an email.

    I’ve got some recommendations for your blog you might be interested in hearing.
    Either way, great site and I look forward to seeing it grow over
    time.

  5. Hi, yup this article is genuinely nice and I have learned lot of things from it
    regarding blogging. thanks.

  6. Thanks on your marvelous posting! I seriously
    enjoyed reading it, you might be a great author.
    I will make sure to bookmark your blog and will often come back later on. I want to encourage you
    to continue your great writing, have a nice day!

  7. gacha porn表示:

    I every time used to study article in news papers but now as I am
    a user of internet so from now I am using net for articles or reviews, thanks to web.

  8. eigenbeleg表示:

    Thermopapierrollen finden in einer Vielzahl von Kassen, Waagen, Automaten oder Bondruckern Verwendung.

    EC-Geräte nutzen ausschließlich Thermorollen 57 mm (57 mm breite
    Rollen aus Thermopapier). Thermo Kassenrollen hingegen gibt
    es in einer Vielzahl verschiedener Breiten, die gängigsten sind hierbei Thermorollen 80 mm und 58 mm.
    Sie können bei GEBONGT24 Thermobonrollen ohne Bisphenol A
    (BPA frei) oder komplett phenolfrei (BP frei) günstig bestellen.
    Grundsätzlich unterscheidet man zwei arten von Bonrollen: Kassen Thermorollen und Kassenrollen aus Normalpapier (Additionsrollen).
    Wann benötige ich Thermorollen? Thermorollen, auch „Thermopapierrollen” oder
    „Thermobonrollen” genannt, benötigen Sie immer dann, wenn Ihre Kasse, Ihr EC-Gerät bzw.

    Ihr Drucker mit einem Thermodruckverfahren arbeitet.
    Beim Druckvorgang wird das Kassenrollen Thermopapier
    partiell erhitzt irgendwas mit das Druckbild aufs
    Papier der Thermorollen “eingebrannt”. Ein einfacher Test,
    festzustellen ob Sie Rollen aus Thermopapier kaufen müssen, ist der Fingernagel-Test.
    Nehmen Sie dazu einfach die aktuelle Thermo Kassenrolle aus Ihrer Kasse bzw.
    Ihrem Drucker und versuchen Sie dabei Fingernagel darauf zu kratzen.

  9. controlc.com表示:

    Awesome! Its actually amazing article, I have got much clear
    idea about from this post.

  10. I’m not that much of a online reader to be honest but your blogs really nice, keep it up!

    I’ll go ahead and bookmark your site to come back in the future.
    Many thanks

  11. Good info. Lucky me I found your site by chance (stumbleupon).
    I have saved as a favorite for later!

  12. This piece of writing will help the internet visitors for setting up new website or even a weblog from start to end.

  13. I know this site presents quality dependent articles or reviews and additional information, is there any other web page which
    offers these kinds of information in quality?

  14. Informative article, just what I wanted to find.

  15. Greetings from Florida! I’m bored to tears at work so I decided to browse
    your blog on my iphone during lunch break. I really like the information you provide here and can’t
    wait to take a look when I get home. I’m shocked at how quick your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyways, wonderful blog!

  16. Hello friends, its enormous piece of writing about
    tutoringand fully defined, keep it up all the time.

  17. I don’t even understand how I stopped up here, however I believed this
    submit used to be good. I don’t recognise who you are but certainly you are going to a well-known blogger in the event you are not already.
    Cheers!

  18. Wonderful blog! Do you have any tips and hints for aspiring writers?
    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you propose starting with a free platform like WordPress or go for
    a paid option? There are so many options out there that I’m totally confused ..
    Any recommendations? Kudos!

  19. I like the valuable info you provide in your articles.
    I will bookmark your weblog and check again here frequently.
    I’m quite sure I’ll learn many new stuff right here! Best of luck for
    the next!

  20. Hello, Neat post. There is an issue along with your web site
    in web explorer, would test this? IE still is the market leader and a big component of other folks will leave out your fantastic writing due to this problem.

  21. Hello my loved one! I wish to say that this post is awesome, great written and come with
    approximately all vital infos. I’d like to peer more posts like this .

  22. Wow, that’s what I was exploring for, what a data! present here at this
    website, thanks admin of this web page.

  23. Nice post. I was checking constantly this blog and I’m inspired!

    Extremely helpful information specifically the ultimate phase :
    ) I handle such information much. I was seeking this particular info
    for a long time. Thanks and best of luck.

  24. kartki表示:

    I used to be recommended this blog by means of my cousin. I’m now not sure whether
    or not this submit is written by way of him as no one else realize
    such special about my trouble. You are wonderful!
    Thanks!

  25. This page truly has all the information and facts I wanted concerning this subject and didn’t
    know who to ask.

  26. What’s up Dear, are you really visiting this site on a regular
    basis, if so afterward you will without doubt take pleasant knowledge.

  27. Hmm it appears like your site 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 blogger but I’m still new to
    the whole thing. Do you have any recommendations for inexperienced
    blog writers? I’d really appreciate it.

  28. Howdy! 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

  29. Today, I went to the beach with my children. 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!

  30. Hello very nice website!! Man .. Beautiful .. Amazing ..
    I will bookmark your site and take the feeds also? I’m glad
    to search out numerous useful information right here within the submit, we’d like develop more strategies
    on this regard, thanks for sharing. . . .
    . .

發佈留言

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