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,233,590 Responses

  1. Trefdzj表示:

    Процесс получения диплома стоматолога: реально ли это сделать быстро?

    doctorsforum.ru/viewtopic.php?f=193&t=40999

  2. Trefnyf表示:

    Легальная покупка диплома ПТУ с сокращенной программой обучения
    samara.listbb.ru/viewtopic.php?f=3&t=668

  3. A Trip Back In Time How People Talked About Mystery Box 20
    Years Ago best online Mystery Boxes

  4. Hi there, I check your blog like every week. Your story-telling style
    is awesome, keep up the good work!

  5. Derekfar表示:

    deneme bonusu veren siteler 2024: deneme bonusu veren siteler yerliarama.org – deneme bonusu veren siteler yerliarama.org

  6. light novel表示:

    Hello! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or maybe guest authoring a blog
    article or vice-versa? My blog goes over a lot of the same topics as yours and I feel we could greatly benefit from each other.
    If you might be interested feel free to shoot me an e-mail.
    I look forward to hearing from you! Fantastic blog by the way!

  7. HassanbuifS表示:

    http://matadorbet.bid/# matadorbet.bid
    deneme bonusu veren siteler denemebonusu2026.com

  8. CarrollUnton表示:

    deneme bonusu veren siteler 2024 https://ultrabet-tr.online/# ultrabet giris

  9. There are various tools and websites that allegation to permit users to view private
    Instagram profiles, but it’s important to edit these following caution. Many of these tools can be unreliable,
    may require personal information, or could violate Instagram’s terms of service.
    Additionally, using such tools can compromise
    your own security or lead to scams. The safest and
    most ethical way to view a private profile is to send a follow
    demand directly to the user. Always prioritize privacy and admiration in your online interactions.

    Here is my web-site – how can you view private instagram accounts

  10. Christel表示:

    We are a group of volunteers and starting a new scheme in our
    community. Your web site offered us with valuable info to work on. You have done an impressive job and our whole community will be grateful to you.

  11. ClydePab表示:

    ultrabet bonus: ultrabet – ultrabet giris

  12. A wonderful post on JS 依照元素出現數量排序 – 馬老師 雲端研究室!
    The insights you shared about Building & Property
    were extremely valuable. It’s clear that you put a lot
    of effort into this. Looking forward to reading
    more of your work. Keep up the great work!

  13. LarryCoell表示:

    guvenilir casino siteleri Canl? Casino Siteleri Casino Siteleri

  14. Derekfar表示:

    en kazancl? slot oyunlar?: slot oyunlar? puf noktalar? – slot oyunlar?

  15. Yes. Therе are no restrictions on whho can theoretically join Arsenal’ѕ academy, but
    it ⅾoes of couгsе provide that you аre goⲟd enough
    for them to᧐ offer you a spot and that you can obtain a permit for
    living in thе UK.

    Feell free to visit my website; ดอกไม้ไว้อาลัย ต่างประเทศ

  16. HassanbuifS表示:

    https://casinositeleri.win/# casino siteleri win
    deneme bonusu veren siteler betturkey

  17. I think what you published made a bunch of sense. However, what about this?
    suppose you composed a catchier title? I ain’t saying your information isn’t
    solid, however suppose you added a headline
    to possibly grab a person’s attention? I mean JS 依照元素出現數量排序 –
    馬老師 雲端研究室 is kinda boring. You should look
    at Yahoo’s front page and watch how they write news titles to grab people to open the
    links. You might add a video or a related picture or two to get readers excited about what you’ve got to say.
    In my opinion, it would make your posts a little livelier.

  18. Triangle Blliards & Bar Stools
    1471 Niswon Ꭱd, Tustin,
    СΑ 92780, United Ⴝtates
    +17147715380
    Buy Pool Table Equipment

  19. Trefbdx表示:

    Как не стать жертвой мошенников при покупке диплома о среднем полном образовании
    files.4adm.ru/viewtopic.php?f=2&t=803

  20. poupart表示:

    Your way of telling all in this article is really nice, all be able to without difficulty understand it, Thanks a
    lot https://en.poupartsyndic.ca/

  21. HassanbuifS表示:

    https://casinositeleri.win/# Deneme Bonusu Veren Siteler
    denemebonusuverensiteler.top

  22. The Reasons To Focus On Improving Fridge Freezer French Doors Uk vinterkall french Door Fridge/freezer review

  23. The 10 Most Terrifying Things About Folding Scooters Heavy Duty electric mobility scooter

  24. LarryCoell表示:

    ultrabet giris ultrabet bonus ultrabet tr online

  25. CarrollUnton表示:

    deneme bonusu veren siteler betturkey betturkey.com https://slot-tr.online/# en kazancl? slot oyunlar?

  26. Can you tell us more about this? I’d like to find
    out some additional information.

    https://w1.prediksinagasaon.cfd/

  27. Since the admin of this website is working, no doubt very soon it
    will be famous, due to its quality contents.

  28. I every time used to read piece of writing in news papers
    but now as I am a user of net thus from now I am using
    net for posts, thanks to web.

發佈留言

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