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,234,817 Responses

  1. bokep indo表示:

    No matter if some one searches for his vital thing,
    so he/she wants to be available that in detail, so that
    thing is maintained over here.

  2. penipu表示:

    It is truly a nice and useful piece of information. I’m happy that you
    simply shared this helpful info with us. Please
    keep us up to date like this. Thank you for sharing.

  3. Keep this going please, great job!

  4. 5 Qualities That People Are Looking For In Every Honda Key Honda Lost Key

  5. continuously i used to read smaller content that also clear their motive,
    and that is also happening with this paragraph which I am reading now.

  6. Thanks for the auspicious writeup. It if truth be told used to be a entertainment account it.
    Look complicated to far introduced agreeable from you!

    By the way, how can we communicate?

  7. KevinSounc表示:

    https://slotsiteleri.bid/# oyun siteleri slot

  8. silebet.com表示:

    Excellent blog right here! Also your website a lot up very fast!
    What web host are you the usage of? Can I get your affiliate link to your host?
    I want my web site loaded up as fast as yours lol

  9. Janeen表示:

    What’s up to every body, it’s my first pay a visit of this
    web site; this website carries remarkable
    and really good material in support of visitors.

  10. HerbertemuRl表示:

    hi guys i translate that https://wplgsturapwmestv185.ru/

  11. sikkimgames表示:

    Spot on with this write-up, I absolutely believe that this web
    site needs a great deal more attention. I’ll probably be back again to see more, thanks
    for the info!

  12. gay_uvpr表示:

    Ставки на спорт для ЛГБТ сообщества | 1xbet для геев: новые горизонты | Gay-friendly 1xbet: ставки с комфортом | Секреты успешных ставок на спорт для ЛГБТ на 1xbet | Гид по ставкам на 1xbet для LGBTQ+
    gsy porn in 1xbet https://1xbetcasinogayclub-ar.com/ .

  13. It’s remarkable designed for me to have a web page, which is useful in support of my know-how.
    thanks admin

  14. Blingle Jacksonville, FL
    10221-3 Beach Boulevard,
    Jacksonville, FL 32246, United Ꮪtates
    9049778928
    Scenic outdoor lighting setups

  15. panda168表示:

    For the perfect experience on our site, you
    should definitely turn on Javascript in your browser. We
    went by hurricane Katrina, that was an expertise! Intrigued by their discovery,
    a good friend of the couple posted photos of their findings on Imgur and
    so they instantly went viral. Monetizing pet sick health into revenue streams:
    exploit the temporal inconsistency of individuals in having high elasticity in pet purchase costs4 however extreme price insensitivity/inelasticity in purchasing pet medical care by freely giving sick pets (particularly
    bulldogs), & getting vet kickbacks or direct revenue-sharing.

    In Nepal’s Taplejung District, red panda claws are used
    for treating epilepsy; its skin is used in rituals for treating sick individuals, making
    hats, scarecrows and decorating homes. Threats identified in Nepal’s Lamjung
    District include grazing by livestock during seasonal transhumance, human-made forest fires and the collection of bamboo as cattle fodder in winter.
    In Nepal’s Rara National Park, Thamnocalamus was
    found in all the droppings sampled, each earlier than and after the monsoon. She stated it was the
    most effective issues she had ever eaten and was savoring each
    chunk!

  16. I know this if off topic but I’m looking into starting my own weblog and was curious what all
    is required to get setup? I’m assuming having a blog like yours would cost
    a pretty penny? I’m not very web savvy so I’m not 100% positive.
    Any recommendations or advice would be greatly appreciated.
    Thanks

    my blog :: lottery defeater software scam

  17. www.nubaq.com表示:

    I got this site from my buddy who shared with me
    concerning this web site and at the moment this time I am visiting this web page and reading very informative articles or reviews at this time.

  18. Good post. I learn something new and challenging on sites I stumbleupon everyday.
    It will always be interesting to read through articles
    from other writers and use a little something from other websites.

  19. Lowell表示:

    Have you ever considered about adding a little bit more than just your
    articles? I mean, what you say is important and everything.
    Nevertheless just imagine if you added some great photos or video clips to give
    your posts more, “pop”! Your content is excellent but with pics and video clips, this site could definitely be one
    of the greatest in its niche. Awesome blog!

  20. Why No One Cares About Mesothelioma Law Mesothelioma Litigation

  21. That is a really good tip especially to those new to the blogosphere.
    Simple but very precise information… Thanks for sharing this one.
    A must read article!

  22. Find Out What Replacement Car Key Audi Tricks Celebs Are Making Use Of Audi Replacement Key Service Near Me

  23. KevinSounc表示:

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

  24. Hello there I am so thrilled I found your webpage, I really found you by
    error, while I was searching on Bing for something else, Anyhow I am here now and
    would just like to say thanks a lot for a fantastic post and
    a all round interesting blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have saved it and
    also included your RSS feeds, so when I have time I
    will be back to read much more, Please do keep up the awesome work.

  25. Amelie表示:

    I think everything wrote was actually very reasonable.

    However, consider this, suppose you wrote a catchier title?
    I am not saying your content isn’t good, however what if you
    added a headline that makes people desire more?
    I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室 is kinda
    vanilla. You could peek at Yahoo’s home page and watch how they create article headlines to
    get viewers to open the links. You might add a related video
    or a picture or two to get people interested about everything’ve got to say.
    Just my opinion, it could bring your posts a little bit more interesting.

發佈留言

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