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

805,508 Responses

  1. Anderson表示:

    Fortunately, you can login to your netsuite login portal [Anderson] account with ease and security by using a netsuite.com customer login. The site
    is listed below. It’s easy to locate it! Here
    are some useful websites to help you get started.

  2. There are a variety of ways to pay in cryptocurrencies. A crypto Gateways gateway is a method to pay with cryptocurrency.
    The crypto payment gateways connect businesses to exchanges for digital money.

  3. You will surely find an amazing model from OnlyFans in Zayla.
    Zayla is a beautiful woman with curves to die.

    You will see her wearing bikinis that are sexually
    attractive in her pictures and videos. Don’t let her curves fool
    your!

    Check out my site: only Fans models

  4. There are a myriad of options available for Buy dogs cbd Uk dog treats.
    There are numerous companies that offer high-quality
    CBD products online. That Pet Care is the perfect product for you if you want a full spectrum CO2 extract.

  5. Bette表示:

    Online gambling has become very popular in the past
    decade. In 1996, there were just fifteen websites.
    By 1997, there were more than 200. According to Frost & Sullivan, online gambling had generated $830 million in profits in 1998.

    Here is my web site; live (Bette)

  6. Double glazing can be fixed if your home has been ravaged by wear and
    wear and tear. This involves fixing the frames of your windows and Bifold doors Lewisham as well as replacing any broken or damaged components.
    Double-glazing systems are also dependent on glass.

  7. If you’re looking for an experienced psychiatrist private, you might be wondering where
    to find the most effective psychiatrist in your private practice.
    This method has numerous advantages which we will go over in this article.

  8. There are many companies that can replace your windows with UPVC windows.

    However, you should make sure you compare quotes from different companies to ensure
    that you are receiving the best price.

    Also visit my website; double glazing Near Me

  9. Window repairs using UPVC double glazing near me me can help
    you keep your home dry. These windows can suffer from
    capillary action that is a frequent problem. This occurs when the hinges and sashes of the
    windows aren’t properly fitted.

  10. Infinite Windows & composite doors St albans Ltd
    offers uPVC windows in St Albans and all of the surrounding areas.
    They’ll give you a no-cost estimate and will show you samples of their windows so that you can decide if they are the right fit for you.

  11. It is best to call a professional UPVC window Repair near me – https://www.anuncios.sitiosuruguay.Com/, service if
    your door requires repairs. They will come and provide the products
    and services that you require. Google Maps makes it easy to
    find an UPVC repair service close to you.

  12. When you are deciding on the best Gummy brand
    for your children There are many factors you need to consider.
    These factors include traceability and the strength of
    the gummy, sugar content, gelatin and sweetness.

    my website: cbd gummies wyoming

  13. You should also take into consideration the time and expertise required to conduct
    the private assessments for adhd in adults assessment.
    In some cases, you can save a lot of money by selecting a more
    specialist clinic.

  14. Denisha表示:

    In this article, we take a look at washer dryer deals
    (Denisha) dryer reviews including the Samsung W90J7400GW, Miele W1,
    and Electrolux EFLS617U. We also examine Samsung’s energy-efficient dryer and its large
    drum portal.

  15. If you’re looking to build a stunning Investor pitch Deck Consultant pitch
    deck, you’re in the right place. The experts at Digital Finance Experts have been providing pitch deck solutions for companies to top companies for years.

  16. glass Fitters表示:

    In search of auto glass fitters
    in my area? We have many options. Here are some of them: Active Auto Glass and Newport Glass.
    Each one of these companies specializes in auto glass repair and replacement.

  17. If you’re thinking of buying a new fireplace You’ve probably been thinking whether a Wall Mounted Fire-mounted fireplace will suit your home.
    It’s crucial to understand the basics of advantages
    and features before making a purchase.

  18. Link Builder表示:

    %%

    Also visit my blog :: Link Builder

  19. If you’re in search of an oven with four slices, you’ve found the
    right spot. We’ve reviewed the different models, discussed their
    strengths and also provided an overview of the best 4 slot Toaster for you.

  20. You should also consider the time and knowledge required to complete
    the private Adult adhd assessment (classifieds.vvng.com)
    assessment. You may be able to save money by choosing an expert clinic in certain cases.

  21. adhd London表示:

    If you’re looking for adhd London testing London There are a variety of different options to choose from.
    Some are more cost-effective than others, so you can have an assessment
    completed quickly.

  22. Aveeno Face Defense is a high-spf moisturiser that comes in a variety of variants.
    Its spf In moisturiser is higher, but it also has Glycerin that attracts and
    holds water in your skin.

  23. The stylish spaceship housing the Panasonic bread 4 Slice toaster best,
    which is four slices wide, is constructed of stainless steel .
    It has a sleek glossy casing. The slots with adjustable widths allow it to accommodate all bread types.

  24. video Poker表示:

    Wow, marvelous weblog format! How long have you ever been blogging for?

    you make blogging glance easy. The entire look
    of your site is magnificent, as smartly as the content
    material![X-N-E-W-L-I-N-S-P-I-N-X]I simply couldn’t
    depart your site prior to suggesting that I really
    enjoyed the usual information a person provide in your visitors?
    Is gonna be back continuously to check up on new posts.

    My site :: video Poker

  25. Double mattresses double bed made of memory foam are an excellent option for those suffering from neck pain or back strain.

  26. Its such as you learn my mind! You seem to understand so much about this, like
    you wrote the guide in it or something. I feel that
    you could do with a few % to pressure the message home a
    little bit, but other than that, this is magnificent blog.
    A fantastic read. I will certainly be back.

    Check out my web blog; double glazing wandsworth

  27. Earning your electrician certificate
    is an excellent way to begin your career in the construction industry.
    A basic understanding of electrical work is necessary to
    become an electrician licensed.

  28. When you’re looking for angel investors South Africa, you should take certain steps to ensure you have a
    strong plan. There are a few points to remember as well as a business plan should
    be in place before presenting your idea.

  29. double glazing Repairs near me repairs to your glazing are essential if your double glazing windows
    or doors have misted. There are many reasons this might happen,
    such as a seal problem or a ventilation problem.

  30. There are many options for portable cheap Wood burners uk burning stoves.

    We’ll be looking at the Grizzly HT3000 and Timberwolf EPA2100.
    Hi-Flame Shetland and Navigator Stoves Works. You
    can also read the customer reviews on the various
    models.

發佈留言

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