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,480,969 Responses

  1. Charlesbot表示:

    https://fastpillseasy.com/# best online ed treatment

  2. Charlesbot表示:

    http://maxpillsformen.com/# Generic Cialis price

  3. See What Fridges & Freezers Tricks The Celebs Are Utilizing fridges & Freezers

  4. 카지노사이트 카지노사이트
    카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트

  5. Having read this I believed it was really informative.
    I appreciate you taking the time and energy to put this informative article together.
    I once again find myself spending a lot of time both reading and leaving comments.
    But so what, it was still worth it!

  6. Just got a magnificent haircut that I definitely like! If you intend to discover an experienced stylist, do not neglect to look into treatment keratin smooth for ideas

  7. Ellen Todd表示:

    Thanks for the clear breakdown. More info at pressure washing Near Me

  8. Diplomi_pzpt表示:

    дипломы купить челны дипломы купить челны .

  9. Charlesbot表示:

    http://fastpillsformen.com/# Viagra without a doctor prescription Canada

  10. mamiqq online表示:

    Good day! I know this is kind of off topic
    but I was wondering which blog platform are you using for this site?
    I’m getting tired of WordPress because I’ve had issues with hackers
    and I’m looking at options for another platform.

    I would be great if you could point me in the direction of a good platform.

  11. Donaldabula表示:

    Tadalafil price buy cialis online Cialis over the counter

  12. sor777表示:

    If some one wants expert view on the topic of blogging and site-building after that
    i propose him/her to pay a visit this weblog, Keep up the nice work.

  13. I do not even know how I ended up here, but I thought this post was great.
    I do not know who you are but definitely you are going to a famous blogger if you aren’t
    already 😉 Cheers!

  14. 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트 카지노사이트

  15. Diplomi_ulpt表示:

    купить диплом в северодвинске prema-diploms.ru .

  16. Stella Rivera表示:

    Do you recognize the value of normal trims? It really makes a distinction in preserving healthy and balanced hair! Look into ulta beauty salon for even more hair treatment ideas

  17. Excellent post. I was checking constantly this blog and I am impressed!
    Extremely useful information specifically the last part :
    ) I care for such information much. I was looking for this certain info for a long time.
    Thank you and good luck.

  18. Ollie Burgess表示:

    If you are traveling Austin, don’t omit out on renting a boat! I found substantive suggestions at Banana Boat Rentals Boat Rental

  19. JeffreyRob表示:

    The 1win promo code: MAX500WIN. This 1Win bonus code 2025 rewards new players with a 500% bonus up to $1025. Valid codes for receiving bonuses without a deposit for new and old users. Hurry to get them today! Activate the promo code 1win and get a generous welcome bonus of up to 500% on your first four deposits.

    https://japasquare.app/blogs/2277/Unlock-Exciting-Opportunities-with-the-1Win-Free-Bonus-Code-2025 – 1win promo code

  20. I didn’t realize how much clutter I had up until I started my scrap removal job. It was frustrating at first, now I feel so relieved 1800 got junk cost

  21. How Techniques With A Network Marketing Program 에볼루션 가품

  22. Diplomi_ybpt表示:

    купить диплом о профпереподготовке prema-diploms.ru .

  23. Diplomi_umpt表示:

    купить диплом с отличием купить диплом с отличием .

  24. Szybka sprzedaż nieruchomości to świetne rozwiązanie dla osób, które potrzebują natychmiastowej gotówki. Dzięki temu procesowi można uniknąć długotrwałych negocjacji i formalności szybka sprzedaż nieruchomości

  25. Lucile Boone表示:

    It’s amazing how chiropractic care can improve overall wellness Chiropractic adjustment for auto injuries

  26. Diplomi_rept表示:

    где можно купить диплом где можно купить диплом .

  27. site here表示:

    Great goods from you, man. I have understand your stuff previous to and you’re just
    extremely fantastic. I really like what you have acquired here, certainly like what you’re saying and the
    way in which you say it. You make it enjoyable and you still take care of to keep it wise.

    I cant wait to read far more from you. This is actually a wonderful site.

發佈留言

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