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...

911,143 Responses

  1. https://bogin1.nyc3.digitaloceanspaces.com/research/bogin1-(475).html
    There often aren’t any set guidelines in relation to MOB outfits for the marriage.

  2. lotto表示:

    Your style is unique compared tօ otһer people I’ve read stuff from.
    I ɑppreciate you fօr posting ᴡhen уou’ve got
    tһe opportunity, Guess I ѡill just book mark this site.

    mү site: lotto

  3. https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/bogin26/research/bogin26-(49).html
    Impressive beading and an alluring neckline make this type worthy of even the fanciest of black-tie weddings.

  4. https://seo29.z20.web.core.windows.net/research/seo29-(62).html
    Most of the combos I characteristic right here come with nice jackets.

  5. Hiya! Quick question that’s totally off topic.
    Do you know how to make your site mobile friendly?
    My web site looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might be able to fix this problem.
    If you have any recommendations, please share. Thank you!

  6. Excellent post. I was checking constantly this blog and I am impressed!
    Very helpful info specially the last part 🙂 I care for such info much.

    I was looking for this particular info for a very long
    time. Thank you and good luck.

  7. I don’t commonly comment but I gotta admit appreciate it for the post on this special one :D.

    Feel free to visit my homepage – kkpoker review – luennemann.org,

  8. Hola! I’ve been reading your blog for a long time now and finally got the courage to go ahead and give you a shout out from
    Houston Texas! Just wanted to mention keep up the great work!

  9. Shaunte表示:

    Awesome! Its truly remarkable piece оf writing, I һave got mucһ clear idea concerning fгom this article.

    Нere is my web-site; casino; Shaunte,

  10. Tracyseils表示:

    acquistare farmaci senza ricetta: Dove acquistare Cialis online sicuro – migliori farmacie online 2023

  11. Roy Raney表示:

    Really nice layout and great written content, absolutely nothing else we require :D.

    Look into my homepage earliest billiard ball (https://coolors.co/u/bida_thinh_kent)

  12. My brother suggested I might like this blog. He was totally right.
    This post truly made my day. You can not imagine just
    how much time I had spent for this info! Thanks!

  13. canadian表示:

    Ꮋello there! I know this is kіnda off tߋpic but I was wondеring if you knew wһere I could locatе a captcha plugin for my comment
    form? I’m using the ѕame ƅlog platform as yours and
    I’m hawving difficulty finding one? Thanks a lot!

  14. http://chinavisa63.s3-website.ap-southeast-4.amazonaws.com/research/chinavisa63-(348).html
    Matching your MOB dress is a fun approach to present you attention to detail.

  15. week表示:

    Attractive component of content. I just stumbled upon your website and in accession capital to say that I get in fact enjoyed account your weblog posts.
    Any way I will be subscribing for your augment and even I fulfillment you
    get right of entry to constantly rapidly.

  16. I don’t even know how I ended up here, but I thought this post was good.
    I do not know who you are but certainly you’re going to a famous blogger if you are not already 😉 Cheers!

  17. casino表示:

    Hello, i feel thɑt і saw ʏօu visited mу weblog ѕo i cаme
    to return thе choose?.I’m attempting tօ fіnd issues to enhance my site!Ӏ guess its ok to
    usе some оf yօur concepts!!

    Review my web-site … casino

  18. https://bogin26.blr1.digitaloceanspaces.com/research/bogin26-(136).html
    Take inspiration from the bridesmaid clothes and speak to your daughter to get some ideas on colors that can work well on the day.

  19. Ronda Jenkins表示:

    https://bogin2.sfo2.digitaloceanspaces.com/research/bogin2-(51).html
    A pink lace Erdem costume, embroidered with pink and crimson blooms, paired perfectly with this modern mom’s half-up, half-down hairstyle.

  20. Eloy Lim表示:

    https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/bogin46/research/bogin46-(397).html
    The mom of the bride costume gallery has a dress for each price range and each physique kind including plus sizes.

  21. Wonderful beat ! I would like to apprentice while you
    amend your website, how can i subscribe for a blog website?
    The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright
    clear concept

  22. https://storage.googleapis.com/bogin5/research/bogin5-(38).html
    Dillards is a superb place to take a glance at if you’re looking for something greater finish.

  23. Irene Torode表示:

    Hello i am kavin, its my first time to commenting anywhere, when i read this post i thought i could also create comment due to this brilliant article.

    Feel free to surf to my web site; pro billiards tips (https://www.wishlistr.com/bidathinhkent/)

  24. August Kim表示:

    https://bogin86.z36.web.core.windows.net/research/bogin86-(28).html
    The beaded metallic tassels on this glimmering gown really came into play when this mom took the dance ground.

  25. https://bogin41.z7.web.core.windows.net/research/bogin41-(17).html
    If you may be unsure, take inspiration from kinds you get pleasure from wearing everyday.

  26. http://chinavisa18.s3-website-eu-west-1.amazonaws.com/research/chinavisa18-(172).html
    The bride’s aunt was simply as modern in a Reem Acra quantity with lace and sheer ruffles.

  27. But a smiling visitor here to share the love (:, btw outstanding pattern.

    My blog post; sex in my area

  28. pocket option表示:

    I am actually grateful to the holder of this web page who has shared this
    fantastic paragraph at at this time.

  29. https://bogin4.sfo2.digitaloceanspaces.com/research/bogin4-(170).html
    But an important piece of attire you may select leading as a lot as your kid’s wedding?

發佈留言

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