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

831,292 Responses

  1. best表示:

    Online gambling has become very popular over the last decade.

    In 1996, there were just fifteen websites. In 1997 there were over 200 websites.

    In 1998 the Frost & Sullivan report stated that online gambling brought in $830 million of revenue.

    Also visit my webpage best

  2. Gambling online has gained a lot of attention in the past decade.
    In 1996, there were only fifteen websites. By 1997, there
    were over 200. According to Frost & Sullivan, online gambling had generated $830 million in revenue in 1998.

    my website: Dealers (http://Www.Artjust.Org)

  3. gpr survey表示:

    Hi tһere would you mond letting me know ԝhich hosting company you’re utilizing?
    I’ve loaded your Ƅlog in 3 competely different Ьrowsers and I must ssay this Ƅlog loads а llot quicker then most.

    Can youu suggest a good web hosting provider ɑt a faіr price?
    Cheers, I apprecіate it!

    Check out my web site; gpr survey

  4. blackjack表示:

    Gambling online has gained popularity over the last decade.

    In 1996, there were only fifteen websites. In 1997 there were over
    200 websites. According to Frost & Sullivan, online gambling had brought in $830 million in revenue in 1998.

    Here is my web page – blackjack

  5. poker表示:

    The Internet has opened up a variety of opportunities for those who enjoy poker.
    The popularity of online poker has grown dramatically in recent years.

    Online poker is an excellent option if you like the game but don’t have time to visit the
    casino.

  6. bets表示:

    New Yorkers can now place online bets on sports since January
    8th 2022. Four main operators have won licenses for mobile betting,
    which allows them to offer their services in different
    ways. The operators are Bally’s and DraftKings NY.

  7. gambler表示:

    There are a variety of treatments for addiction to gambling.
    One-on-one counseling, medication, lifestyle changes and medications are all possible options.
    When you cannot stop yourself from engaging in this behavior, it becomes an addiction.

    Also visit my blog :: gambler

  8. blog bdsm表示:

    Do you have a spam issue on this blog; I also am a blogger, and I
    was curious about your situation; many of us have created
    some nice procedures and we are looking to exchange techniques with other folks,
    be sure to shoot me an e-mail if interested.

  9. bets表示:

    A player can select the theme they would like to play and pick from a variety of online slots, including classics
    and more recent branded releases. The payouts are different but they all
    adhere to the same mathematical formula.

    Here is my site: bets

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

  11. Poker online is legal. However you must verify that you’re legally able to
    play. The majority of online poker sites require players
    to be at least 18 years of age, or at the minimum, the
    legal drinking age for your area of residence.

    Also visit my page blackjack, neuromuscular–occlusion-info.cdn.ampproject.org,

  12. BenTence表示:

    hydroxychloroquine for arthritis reviews fauci emails hydroxychloroquine side effects hydroxychloroquine 200 mg countries that use hydroxychloroquine

  13. gaming表示:

    Online slots are a favorite entertainment for millions
    of players. This fun and exciting kind of gambling can be an excellent way to have fun or earn extra money.
    There are many games to choose from and even free games.

    my website :: gaming

  14. best表示:

    Sports fans in New York are allowed to place bets online starting January.
    8 2022. Four major operators have been granted licenses for mobile betting.
    They will be able to provide their services in a variety ways.

    my web-site … best

  15. Definitely believe that which you stated. Your favorite reason seemed to
    be on the net the simplest thing to be aware of.

    I say to you, I certainly get annoyed while people consider worries that they just don’t know about.
    You managed to hit the nail upon the top as well as defined
    out the whole thing without having side-effects , people can take a
    signal. Will likely be back to get more. Thanks

  16. %%

    Have a look at my homepage :: 200ml joop Homme

  17. ttlink.com表示:

    %%

    Also visit my web page – bunk Bed with double bed (ttlink.com)

  18. Gambling online is governed by many rules and regulations.
    In the US, for example you cannot base an online gambling
    website in the country in which you reside however, you are able to base it
    elsewhere.

    Feel free to surf to my page; gambler (6Umxynwuohtdp7pmbveifzutkim2dceesbmdybladbbxwgy3plha.cdn.ampproject.org)

  19. daisy 100Ml表示:

    %%

    Also visit my blog post: daisy 100Ml

  20. slots表示:

    The player can pick the theme they prefer and select
    from a wide selection of online slot machines, including
    classics and more recent branded releases. The payouts can vary but they
    all adhere to the same mathematical formula.

    Take a look at my page: slots

  21. sbo表示:

    When I originally commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I
    get three emails with the same comment.
    Is there any way you can remove people from that service?
    Thanks!

發佈留言

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