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

806,687 Responses

  1. If you are considering the possibility of a private adhd assessment London assessment in London, there are
    a few things you need to consider before making a final decision.

  2. If you’re in search of a nourishing SPF 50 moisturiser,
    then you’ve come to the right spot. The Sunny Daze SPF 50 Moisturizer is a combination of
    UVA and UVB protection and is a rich source of vitamin E and
    zinc oxide.

  3. Howdy! I realize this is sort of off-topic but I
    had to ask. Does running a well-established blog like yours require a large
    amount of work? I am completely new to operating a blog however I do write in my journal everyday.
    I’d like to start a blog so I will be able to share my own experience and thoughts online.

    Please let me know if you have any suggestions or tips for brand new aspiring blog owners.
    Thankyou!

  4. Sherman表示:

    Online gambling has become extremely popular in the last 10 years.
    In 1996, there were just fifteen websites.
    In 1997, there were over 200. According to Frost & Sullivan, online gambling had generated $830 million in profits in 1998.

    Here is my web blog games (Sherman)

  5. Before you bite into your favorite gummy bear you should know what is in them.
    Cannabidiol Gummy For Sale bears are made of gelatin,
    sweeteners, and colorings. Let’s look at the ingredients listed below to
    see how they are made.

  6. There are many benefits to getting a full diagnosis of ADHD and Autism from an expert in Private Adhd Diagnosis Scotland.
    It’s not as crucial to be separated from the specialist as you think.

  7. I gotta bookmark this web site it seems very beneficial very helpful.

    Have a look at my web page :: download psp games for free

  8. Quotatis can help you find window installers in your area in case you are searching
    for an installation company for your windows or doors.
    This website is free and connects you to local window Front door fitters near me,
    and it is a matter of seconds to sign up.

  9. There are many options available when it comes to purchasing buy Delta 8 flowers For sale Eight
    flowers.

  10. Hi, its nice post concerning media print, we all be aware of media is
    a fantastic source of data.

  11. A pitch deck consulting services deck
    consultant service is a great way of getting a jump into your next funding round.

  12. Purchase high-quality Strongest cbd uk oil
    from trusted brands when you are shopping for it. Although the UK CBD industry is not fully controlled however,
    you are able to enjoy the benefits of certain regulations in the country.

  13. UPVC windows aren’t easy to maintain. In order to keep them looking good
    as new, it is recommended to regularly clean them using an easy
    cloth and soapy water. You can also apply rust-resisting oil to the
    frame.

    My web site; glass replacement near me

  14. A toaster that is slow, like the Graef is the best 4 Slice Toaster option if you’re looking to buy a
    four-slice model. Despite its slow speed it can make perfect toast and the German design will enthral you.

  15. If you are looking for a Psychiatrist Private, you could be wondering where
    to find the most effective psychiatrist in your private practice.
    There are many benefits to this approach that you can learn about in this
    article.

  16. There are many advertisements on the internet for penis enlarger pump.
    Many men are curious about how they can make their
    penis bigger, and over 60 000 searches for “penis enlarger Pumps Online
    expanding” are performed each month.

  17. The ADHD Centre is home to a team that includes psychologists and Adhd Clinic london Behavioural Coaching Coaches.
    Its goal is to improve the understanding of science behind ADHD
    as a neurological disorder that can affect people
    all through their lives.

  18. You may be looking for a psychiatrist but aren’t sure which is the best psychiatry Private practice psychiatrist.
    This strategy has many advantages which we will cover in this article.

  19. Ulrike表示:

    If you’re in search of the top american fridge freezer (Ulrike) available in the UK,
    there are some things you should take into consideration.

  20. There are many ways to pay using cryptocurrencies. One option is using the crypto
    gateway. A crypto payment gateways
    payment gateway connects merchants with digital money exchange.
    To accept cryptocurrency merchants do not require a new account.

  21. If you’re seeking information on repairs to sash windows check out this
    article. We’ll talk about the most common problems, costs, and
    requirements to draughtproof a sash window Repairs.

  22. Slots表示:

    Online slots are a favored pastime for millions of players.
    This fun and exciting kind of gambling can be a great way for you to enjoy yourself or earn extra cash.
    There are plenty of different games to play and
    there are free games you can play.

  23. There are many body moisturiser With spf moisturisers containing SPF on the market,
    and picking the best one could be an overwhelming choice.
    This article will review the benefits of ingredients, the cost, and the range of some
    of the most popular choices.

  24. Ставки на спорт , Ставки на киберспорт/Sports betting,
    online casino deposit bonuses 100%
    Registration+500% Bonus First deposit

  25. Moisturizer with SPF30 offers long-lasting sun protection. It reduces imperfections and
    decreases melanin production. It also helps to hydrate and
    nourish skin. Its natural color is similar to a blush, and it is suitable spf moisturiser for face [https://dmokgi3.cafe24.com/bbs/board.php?Bo_table=free&wr_id=4449] all skin types.

  26. Penis enlarger pump Near me enlarger pump available for sale is something you
    may have heard of. But have you ever thought about whether they really work?

  27. The internet is a great source of information, and the world of online gambling is different.
    Online game operators are offering more bonuses
    and file spins as people are discovering the fun and excitement
    of online judi slot gacor
    games.

  28. If you’re looking for a simple way to make your penis Enlarger Pump near me bigger or a more efficient
    performance-enhancing tool for your performance, a Penis larger pump could be a great option.

  29. If you’re Double Glazing In Luton need of an upgrade to your window in Luton you’ll need
    to locate a company that offers a an extensive range of
    services.

發佈留言

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