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,124 Responses

  1. You’ve come across the right site if you’re looking to buy
    quality windows made of Aluminium. commercial glazing st albans Albans Aluminium Windows are available in a variety styles and colors.

    They are made to BS specifications.

  2. Sexdolls sale表示:

    If you are shopping for a sexdoll for a trustworthy vendor who offers a
    money-back guarantee with detailed information on the manufacturer, detailed photos of
    the exact sex doll, and customisation.

    My web-site … Sexdolls sale

  3. It isn’t easy to identify adhd in Women Adult in women. Women often have the burden of being misunderstood, unappreciated, and feeling
    like a failure especially if the diagnosis is delayed.

    Typically, women are seen as having a disorder or co-occurring condition.

  4. When looking for windows and doors in my area it is best
    to search for a company with an established reputation for quality work.
    A business that employs many people and has a dedicated staff
    is more likely to remain in operation for a
    long time.

    Here is my blog – Window Glass Replacement Near Me

  5. When it comes time to purchase best delta 8 hemp flowers for sale Eight flower,
    you’ve got a variety of options.

  6. Sam表示:

    There are many models of toasters with wide slot 2 slice toaster (Sam) slices that are suitable for use at home.
    There are a myriad of options to choose from including crumb
    tray with high lift and sensors.

  7. Repairs to windows made of upvc window repairs near me near me can help keep your home dry.
    There are some common problems that arise with the windows, including capillary action. This
    happens when the window’s sashes and hinges aren’t properly fitted.

  8. There are many glass repair businesses in Luton. But there are
    some that stand out above the others. Here are a
    few of the top. Cassidy double Glazing luton
    and Locks, for instance, is one of these companies.

  9. There are a lot of options for wall mounted electric Fireplace-mounted
    fireplace electric options. You can choose from Napoleon Allure, Winslow, or Prism.

    You can also pick flicker lighting if you require more variety.

  10. Pretty section of content. I just stumbled upon your blog and in accession capital how to download music to iphone
    assert that I acquire actually enjoyed account your blog posts.
    Anyway I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  11. Although you can induce your man to have sexual activity using sex toys
    but only 45% of sextoys For men who
    actually use them. These toys are an excellent present that
    can improve your relationship with your man.

  12. If you’ve ever wondered how to Accept Cryptocurrency Payments
    crypto payments, you’re at the right spot.

  13. If you’re looking to have your car mobile Key Programming reprogrammed , or not,
    there are a variety of options to locate a locksmith in your
    area who can help you. Certain services are manual, while others require the assistance of
    an expert.

  14. To make your purchase as thrilling as you can, you can even personalize the
    doll. You can change her hair color, eye color,
    body type, and much more. You can also alter certain details of the doll’s anatomy.

    My web page sexdoll for men

  15. It isn’t easy to locate an electrician close to your home however it is doable.
    Ask around and look online to read reviews that can help
    you locate the best electrician.

    My website; Electricians Near me

  16. DavidJex表示:

    metformin 104 pill metformin 850 mg india

  17. If you’re looking for the most popular perfumes for women then you’re in the right place.
    This article will talk about some of the most popular scents available.

    There is also information about other popular scents that are best unisex perfumes-friendly.

  18. Sexdolls sale表示:

    Adult sexy dolls look extremely realistic, and
    many of them come with a real anal cavity, mouth, and vagina.
    Some dolls have cleavage or genitals to make them
    appear more real.

    Have a look at my blog post Sexdolls sale

  19. Hey there! I could have sworn I’ve been to this site before but after reading through some of the post
    I realized it’s new to me. Anyways, I’m definitely glad
    I found it and I’ll be bookmarking and checking back often!

    Here is my blog :: best onlyfan Account

  20. Private online ADHD assessments are available. If you’re a parent of
    a child who has Adhd private assessment or other disorders, you might be
    worried about visiting an Psychologist for the first time.

  21. A local electrician Near me
    is the best choice if you need to have electrical work completed at your home.

    The reason for this is that they have the necessary experience and they can respond quickly to emergency situations.

  22. There are many options available to those who are looking to pursue working in the mental health sector.
    You can visit your local doctor’s clinic or attend job fairs to
    search for the right job.

    Review my website … Online Psychiatrist

  23. There are a few things to consider when you’re looking
    for the top Lg American Fridge Freezer
    fridge freezers available in the UK.

  24. When you are deciding on the best cbd cartridge Refill cartridge There are many aspects to
    think about. The majority of these cartridges are
    made of 304-grade stainless steel that is free from any additives.

  25. Toaster sale表示:

    If you’ve ever had trouble cooking breakfast or need to make a
    quick sandwich, a Toaster sale is one of the best options.
    They’re cheap and easy to use and can make delicious toast and other foods.

  26. There are many options available for window repairs in SE13.
    It is crucial to choose a certified and insured professional company.
    Different states require different license types. It is important to check with your local regulatory
    body.

    Look into my web site: Lewisham Glass repair

  27. The most effective American fridge freezers are built with style in your mind.
    American fridge freezers feature two doors with side-byside arrangements that make loading and unloading them an easy task.

  28. Celina表示:

    Poker players have plenty of alternatives thanks to the
    internet. Poker online has seen a rapid increase in popularity in the past
    few years. Online poker is an excellent option for those who enjoy the
    game but don’t have time to go to casinos.

    My web site; dealers (Celina)

  29. If you’re looking for a nutritious best spf face moisturiser 50
    moisturiser, you’ve come to the right spot.

    Sunny Daze SPF50 Moisturizer provides both UVA and UVB protection,
    and is rich in vitamin E and zinc oxide.

發佈回覆給「american fridge Freezers」的留言 取消回覆

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