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,155,875 Responses

  1. Hanna表示:

    This web page was final edited on July 28, 2023 at 4:52 am.
    The Altmühlsee Run takes place on the third Saturday in July and the Altmühlsee
    Festival open-air theater sequence takes place in the summer time months.
    For tourist functions, the MS Brombachsee, a trimaran whose architecture and building is exclusive in Europe, operates on the Großer Brombachsee inside the summer
    months between the piers Ramsberg, Absberg, Enderndorf, Allmannsdorf and Pleinfeld.
    The Birkenhof north of Absberg between the two bodies of
    water Igelsbach and Gänsbach, whose corridor additionally sank into the Großer Brombachsee,
    solely existed as a barn by 1962. The mill building with
    the related farm had a pond and was situated within the Brombachtal instantly on the Brombach between the Birkenmühle and Langweidmühle
    properties, which were also misplaced as a result of the construction of the Großer Brombachsee.
    The width of the trench is between ten and 15 meters, the depth is 2.50
    meters on the east side, otherwise around 1.50 meters.
    A second ditch at a depth of six meters separated this part of the castle from the core castle, the
    place there is a tower hill and several partitions. Solely the tower hill with wall and ditch is
    preserved from the thing protected as a floor monument.
    Tower hill discovered two buildings “about 17 by 17 meters tall”, they
    served as a forest house.

  2. I waѕ suggested tһis web site ƅy my cousin. Ӏ’m not sure ᴡhether this post is written by һim as no one elsе қnow such detailed ɑbout my difficulty.
    Υou are incredible! Ƭhanks!

    Lοߋk intօ my webpage – Arena333 Slot Login Link Alternatif

  3. kontol表示:

    What’s up, just wanted to say, I loved this article. It
    was inspiring. Keep on posting!

  4. Fantastic beat ! I wish to apprentice while you amend
    your site, how can i subscribe for a blog site? The
    account aided me a acceptable deal. I had been tiny bit acquainted of this your
    broadcast offered bright clear idea

  5. 5 Killer Quora Answers On Charity Shop Online Clothes Uk charity shop online Clothes uk

  6. Having read this I believed it was rather informative.
    I appreciate you finding the time and energy to
    put this short article together. I once again find
    myself personally spending a significant amount of time both reading and leaving comments.
    But so what, it was still worth it!

  7. Hurrah, that’s what I was exploring for, what a stuff!
    present here at this weblog, thanks admin of this web page.

  8. How Erb’s Palsy Case Was The Most Talked About Trend In 2023 Erb’s palsy lawyer

  9. boy789表示:

    Your style is very unique in comparison to other people I’ve
    read stuff from. Many thanks for posting when you’ve got the opportunity, Guess I will just book
    mark this blog.

  10. Adriana表示:

    lgbt porn

  11. Jonh表示:

    homosexual porn

  12. See What Amazon Uk Online Shopping Clothes Tricks The Celebs
    Are Utilizing amazon uk online shopping clothes

  13. You’ll Never Be Able To Figure Out This Window Repair Near
    Me’s Benefits window repair near me

  14. Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your web site is magnificent,
    as well as the content!

  15. Meds information. Short-Term Effects.
    can i buy bactrim without insurance
    Best information about medication. Get information now.

  16. RobertNat表示:

    Luis Alberto Suarez Diaz https://luis-suarez.prostoprosport-br.com Uruguayan footballer, striker for Inter Miami and Uruguay national team. The best scorer in the history of the Uruguay national team. Considered one of the world’s top strikers of the 2010s

  17. Drug information for patients. Effects of Drug Abuse.
    can i order generic diflucan tablets
    All about medication. Get information here.

  18. Mobile Slots It’s Not As Hard As You Think exciting slots

  19. Von表示:

    9 Things Your Parents Taught You About L Shaped Beds For Small Rooms l shaped bed – Von

  20. Medicines prescribing information. Effects of Drug Abuse.
    how to get cheap fluvoxamine prices
    Some trends of medicament. Read here.

  21. I’m not sure why but this weblog is loading very slow
    for me. Is anyone else having this problem or is it a problem on my
    end? I’ll check back later and see if the problem still exists.

  22. Glycogen is a form of stored glucose that serves as the primary energy
    source for the body during exercise and physical activity.
    It is stored in the muscles and liver and is broken down into glucose when the body needs
    energy.

    Browse my internet site; Glycogen Support Nutrition

  23. Somebody necessarily assist to make significantly articles I might state.

    That is the first time I frequented your web page and up to now?
    I amazed with the research you made to create this particular post amazing.
    Wonderful job!

  24. vn8823767表示:

    Heya i am for the first time here. I came across
    this board and I in finding It truly useful
    & it helped me out a lot. I hope to provide something again and help others such as you
    aided me.

發佈留言

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