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

910,327 Responses

  1. blackjack表示:

    Gambling online has gained a lot of attention in the past decade.
    In 1996, there were only fifteen websites. In 1997, there were more than 200.
    In 1998 the Frost & Sullivan report stated that online gambling generated $830 million in revenues.

    Here is my web blog … blackjack

  2. %%

    Feel free to surf to my homepage – Ck One Perfume

  3. %%

    Have a look at my web blog – unisex Perfume

  4. gambling表示:

    Online gambling has become
    extremely popular in the past decade. In 1996, there were only fifteen websites.
    By 1997, there were over 200. In 1998 an Frost & Sullivan report stated that
    online gambling brought in $830 million in revenue.

  5. The internet betting market offers both bookmakers as well as bettors many benefits.
    Online betting sites allow people to bet on their favourite sports, and also earn them money.
    These sites offer a variety of ways to deposit money.

    my homepage – slots (https://apps.thinkhr.com/api/v1/throne/sso.Json?SSOID=flex&AuthCode=FA93F344FDCB60D46F089F99A5FA3569MTg3NjI0&thrsso=1&redirect=https%3A%2F%2Fbricksofnorthville.com)

  6. MichaelMuh表示:

    how to get on dark web dark web login

  7. food表示:

    I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored material
    stylish. nonetheless, you command get got an nervousness over that
    you wish be delivering the following. unwell unquestionably come more formerly again as
    exactly the same nearly a lot often inside case you shield
    this hike.

  8. live表示:

    Poker is legal online. However you must make sure you’re legally able to play.

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

    My web-site … live

  9. Marina表示:

    Online casinos offer many benefits. It isn’t necessary to spend a
    lot of money. Many online casinos offer free games and players can test them to determine which games they like best.

    My web-site; poker (Marina)

  10. MichaelMuh表示:

    how to access dark web darknet site

  11. bets表示:

    There are many reasons to engage in betting on sports, ranging from the possibility
    of making a profit to the fact that it adds excitement and a reason to look forward to the overall viewing
    experience.

    Also visit my web page :: bets

  12. blackjack表示:

    The popularity of online gambling has increased in the past decade.
    In 1996, there were just fifteen websites. In 1997,
    there were over 200. In 1998 an Frost & Sullivan report stated that
    online gambling brought in $830 million of revenue.

    My blog post – blackjack

  13. best表示:

    Online gambling is governed by various rules and regulations.
    In the US for instance it is not possible to establish an online gambling
    site in the country where you live however, you are able to base it elsewhere.

    Look at my blog post: best

  14. MichaelMuh表示:

    how to get on dark web deep dark web

  15. Hello, its nice paragraph about media print, we all understand media is
    a enormous source of data.

  16. gaming表示:

    There are a myriad of rules and rules that govern online gambling.

    For instance in the US it is not possible to have gambling websites
    that are located in your home country. However, it could be established elsewhere.

    Here is my blog post: gaming

  17. All our deliveries are completely insured and
    expertly packed.

    Feel free to visit my page – Baccarat Hotel nyc

  18. blackjack表示:

    Online gambling has grown in popularity 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.

    Here is my website – blackjack

  19. online表示:

    You can pick the theme you like and play hundreds of online
    slots. The payouts can vary but they all adhere to the
    same mathematical formula. Look for the “Auto Spin” option to find the most suitable slot game.

  20. %%

    Feel free to visit my web-site; triple bunkbed

發佈留言

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