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,234,968 Responses

  1. Darrelherse表示:

    slot siteleri 2024: yeni slot siteleri – deneme bonusu veren siteler

  2. bokep jepang表示:

    Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading through your blog posts.

    Can you suggest any other blogs/websites/forums that go over the same subjects?
    Thanks a lot!

  3. Elizabet表示:

    This article gives clear idea for the new viewers of blogging, that genuinely how to do
    running a blog.

  4. Darrelherse表示:

    slot oyunlar? siteleri: deneme bonusu veren slot siteleri – deneme veren slot siteleri

  5. Pretty nice post. I just stumbled upon your blog and wanted to mention that
    I have really loved browsing your blog posts. After all I will be
    subscribing in your rss feed and I am hoping you write once more soon!

  6. Ev Taşıma | Ev taşıma sürecinde profesyonel bir firma seçmenin önemini vurgulayan bu makaleyi ilgiyle okudum. Kozcuoğlu Ev Taşıma olarak, müşterilerimize güvenilir bir taşınma deneyimi sunmayı taahhüt ediyoruz.

  7. Darrelherse表示:

    slot casino siteleri: slot siteleri guvenilir – slot casino siteleri

  8. How Much Can Replacement Key For Smart Car Experts Earn? smart forfour Key Replacement

  9. Side Games Are Some Of The Advantages Of Online Bingo
    프라그마틱 환수율

  10. KevinSounc表示:

    http://denemebonusuverensiteler.win/# bonus veren siteler

  11. Great blog! Is your theme custom made or did you download it from
    somewhere? A design like yours with a few simple tweeks would really make
    my blog jump out. Please let me know where you got your design. Thanks

    Feel free to visit my site :: pronerve 6 scam

  12. Darrelherse表示:

    deneme bonusu veren siteler: slot siteleri – slot siteleri 2024

  13. I believe what you said was very reasonable. However,
    think on this, suppose you wrote a catchier
    title? I ain’t suggesting your content isn’t solid., however what if you added a title that makes
    people desire more? I mean JS 依照元素出現數量排序 – 馬老師
    雲端研究室 is a little boring. You should look at Yahoo’s
    home page and note how they write news titles to get viewers to
    open the links. You might add a related video or a picture or two
    to get people interested about what you’ve written. In my opinion,
    it could bring your posts a little livelier.

  14. suitehire.com表示:

    excellent points altogether, you just gained a brand new reader.
    What might you suggest about your post that you made a few days in the
    past? Any certain?

  15. mezoterapi表示:

    mezoterapi

    You have made some decent points there. I looked on the net for additional information about the issue and found most individuals will go along with your views on this web site.

  16. Lilly表示:

    Hi! Do you know if they make any plugins to help with Search Engine Optimization?
    I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very
    good success. If you know of any please share. Thanks!

  17. Darrelherse表示:

    slot siteleri guvenilir: slot oyun siteleri – guvenilir slot siteleri

  18. It’s appropriate time to make some plans for the future and
    it is time to be happy. I’ve read this post and if I could I
    wish to suggest you few interesting things or suggestions.
    Perhaps you could write next articles referring to this article.
    I desire to read even more things about it!

  19. I am sure this paragraph has touched all the internet people, its really really nice post on building up new blog.

  20. Darrelherse表示:

    yasal slot siteleri: en iyi slot siteleri – slot casino siteleri

  21. Tory表示:

    I blog quite often and I truly thank you for your information. The article has truly peaked my interest.

    I am going to take a note of your site and keep checking for new information about once
    a week. I subscribed to your Feed too.

  22. Darrelherse表示:

    deneme veren slot siteleri: en iyi slot siteleri 2024 – oyun siteleri slot

  23. the rose toy表示:

    Admiring the time and effort you put into your website and in depth
    information you provide. It’s good to come across a blog every
    once in a while that isn’t the same outdated rehashed material.
    Excellent read! I’ve saved your site and I’m adding your RSS feeds to
    my Google account.

  24. The information provided on this site is comprehensive and reliable.

  25. Today, I went to the beachfront with my kids. 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 put 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 entirely off topic but I had to tell
    someone!

  26. Unexpected Business Strategies That Aided Audi A3 Replacement
    Key Succeed audi a3 spare key

  27. Syair Macau表示:

    You ought to take part in a contest for one of the greatest websites on the net.
    I am going to highly recommend this blog!

  28. Darrelherse表示:

    slot siteleri bonus veren: slot bahis siteleri – deneme bonusu veren siteler

發佈留言

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