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

  1. Невроз – это нервно-психическое расстройство, развившееся в ответ на стресс или психологическую травму. Читать статью на Остеодок.
    Невроз: симптомы и причины – в статье Дмитрия Котова

  2. KevinSounc表示:

    http://slotsiteleri.bid/# yasal slot siteleri

  3. Профессиональный сервисный центр по ремонту холодильников и морозильных камер.
    Мы предлагаем: мастера по ремонту холодильников
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  4. KevinSounc表示:

    https://slotsiteleri.bid/# guvenilir slot siteleri

  5. KevinSounc表示:

    https://slotsiteleri.bid/# slot oyun siteleri

  6. Hi there, just wanted to say, I loved this article.

    It was funny. Keep on posting!

  7. sendokjp表示:

    It’s going to be finish of mine day, however before ending I am reading
    this fantastic article to increase my know-how.

  8. Darrelherse表示:

    en iyi slot siteleri 2024: deneme bonusu veren siteler – slot siteleri guvenilir

  9. I believe everything published was actually very logical.
    However, consider this, what if you typed a catchier post title?
    I mean, I don’t want to tell you how to run your blog, however what if you added a headline that grabbed people’s
    attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is a little
    plain. You might glance at Yahoo’s home page and watch how they create post titles to get viewers to
    open the links. You might add a video or a related picture or two to grab readers interested about everything’ve got to say.
    Just my opinion, it would make your posts a little bit more interesting.

  10. Darrelherse表示:

    en guvenilir slot siteleri: en iyi slot siteler – deneme bonusu veren siteler

  11. If some one desires to be updated with hottest
    technologies after that he must be pay a quick visit this site and
    be up to date every day.

  12. Darrelherse表示:

    bonus veren slot siteleri: yasal slot siteleri – deneme bonusu veren slot siteleri

  13. Hi, I do think this is an excellent site.
    I stumbledupon it 😉 I will come back yet again since I saved as a
    favorite it. Money and freedom is the greatest way to change, may
    you be rich and continue to guide other people.

  14. Darrelherse表示:

    slot siteleri guvenilir: slot siteleri 2024 – slot siteleri guvenilir

  15. visit website表示:

    Right here is the perfect webpage for anybody who really wants to find out about this topic.
    You understand so much its almost hard to argue with you (not that I
    really would want to…HaHa). You certainly put a new spin on a topic which has been written about for decades.
    Wonderful stuff, just great!

  16. Darrelherse表示:

    casino slot siteleri: deneme veren slot siteleri – slot siteleri 2024

  17. The high 5 friday cash jackpot starts with $72,000 on Friday, September 6th, at
    https://lickystarcasino.in/ 2024!
    From 18:00 up to ten:00 every Tuesday!

  18. japanese porn表示:

    Hurrah! Finally I got a web site from where I can in fact obtain helpful data concerning my
    study and knowledge.

  19. I don’t know whether it’s just me or if everybody else experiencing problems
    with your website. It appears like some of the text on your content
    are running off the screen. Can somebody else
    please provide feedback and let me know if this
    is happening to them too? This could be a issue with my internet browser because I’ve had
    this happen before. Thanks

  20. I’m really enjoying the theme/design of your site.
    Do you ever run into any internet browser compatibility problems?
    A number of my blog audience have complained about my site
    not operating correctly in Explorer but looks great in Firefox.

    Do you have any suggestions to help fix this problem?

  21. I think what you posted made a ton of sense. However, what
    about this? suppose you added a little information? I mean, I don’t wish to tell you how to run your blog, however what if you added a headline
    that grabbed folk’s attention? I mean JS 依照元素出現數量排序 –
    馬老師 雲端研究室 is a little vanilla.

    You could glance at Yahoo’s front page and note how they create
    post headlines to get people interested. You might add a video or a related picture or two to get readers interested about
    what you’ve written. In my opinion, it would bring your posts a little bit more interesting.

  22. Darrelherse表示:

    deneme bonusu veren siteler: slot siteleri 2024 – canl? slot siteleri

  23. In recent years, CBD products have surged in popularity, offering a natural
    alternative to traditional medicine for ailments such as anxiety, pain, and insomnia.

  24. dewa scatter表示:

    Thanks for every other informative blog. Where else may
    I get that type of information written in such
    a perfect method? I have a venture that I am simply now running on, and I have been at the glance out for such information.

  25. Darrelherse表示:

    en iyi slot siteleri 2024: yasal slot siteleri – slot siteleri bonus veren

  26. look at this表示:

    Hi there! 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. Many thanks!

  27. Hello there! This post couldn’t be written much better!
    Looking through this post reminds me of my previous roommate!
    He always kept talking about this. I most certainly will send this post to him.
    Fairly certain he will have a very good read.
    Many thanks for sharing!

  28. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza

  29. Evden Eve Nakliyat | Makalede bahsettiğiniz taşınma sürecine dair öneriler, müşterilerimizin bilinçli kararlar almalarına yardımcı olacak nitelikte. Kozcuoğlu Evden Eve Nakliyat olarak, bu süreçte yanlarında olmaktan mutluluk duyuyoruz.

發佈留言

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