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,123,006 Responses

  1. Keep on working, great job! cursilloscolombia.org

  2. Attractive section of content. I just stumbled upon your blog and in accession capital
    to assert that I acquire actually enjoyed account your blog posts.
    Anyway I will be subscribing to your feeds and even I achievement
    you access consistently quickly.

  3. Darrelherse表示:

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

  4. KevinSounc表示:

    http://slotsiteleri.bid/# deneme bonusu veren siteler

  5. Darrelherse表示:

    casino slot siteleri: deneme bonusu veren slot siteleri – canl? slot siteleri

  6. Hi there would you mind letting me know which
    webhost you’re working with? I’ve loaded your blog in 3 completely different internet browsers
    and I must say this blog loads a lot faster then most.
    Can you recommend a good internet hosting provider at a fair price?
    Many thanks, I appreciate it!

  7. mirax表示:

    Hi! Do you want to get 0.001 BTC for free????
    I found an opportunity to get them with the help of Mirax casino (find
    it in the search engine), you need to register there and when registering enter the bonus code CHIPMX, the account will be credited bonus 0,0001 BTC which can be withdrawn if you
    make X10, I did it and I withdrew, try and you!

  8. Darrelherse表示:

    yeni slot siteleri: canl? slot siteleri – slot casino siteleri

  9. Darrelherse表示:

    en iyi slot siteleri 2024: slot bahis siteleri – yeni slot siteleri

  10. Victor表示:

    It’s very simple to find out any matter on web as
    compared to books, as I found this paragraph at this web page.

  11. KevinSounc表示:

    https://sweetbonanza.network/# sweet bonanza taktik

  12. Darrelherse表示:

    slot oyunlar? siteleri: bonus veren casino slot siteleri – slot siteleri 2024

  13. The Most Underrated Companies To In The Hyundai Spare Key Industry
    How Much Does A Hyundai Key Fob Cost

  14. Darlene表示:

    Guide To Queen Size Pull Out Couch: The Intermediate Guide In Queen Size Pull Out Couch queen size pull out couch – Darlene,

  15. Mostbet表示:

    I’m not sure exactly why but this web site is loading extremely slow for me.
    Is anyone else having this problem or is it a issue on my end?

    I’ll check back later and see if the problem still exists.

  16. Нellⲟ there! This is my first visit t᧐ yoսr blog!
    We are a team of volunteers ɑnd starting a new project
    іn a community in tһe same niche. Your blog рrovided ᥙs usеful information tο work on.
    You have ɗone a wonderful job!

    Feel free tо surf to my blog PokemonToto Alternatif

  17. Appreciate it, Loads of facts!

  18. Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
    Мы предлагаем: профессиональный ремонт ipad
    Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!

  19. Drug information for patients. Drug Class.
    generic pregabalin vs lyrica
    Some about medication. Get information now.

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

  21. KevinSounc表示:

    http://denemebonusuverensiteler.win/# deneme bonusu veren siteler

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

  23. 10 Inspirational Graphics About Nissan Key Replacement Prices nissan Qashqai Key replacement uk

  24. this is not exclusively the https://cheapairportparkingjfk.com/, but and save you need a lot time. another option that will save the customer from a huge amount of troubles, is to ask about this relative or a friend.

  25. 45.4.175.178表示:

    The 9 Things Your Parents Taught You About Replacement
    Smart Car Key replacement smart car key (45.4.175.178)

發佈留言

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