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

902,537 Responses

  1. Replace getting this done. Immediately replace the thought with said
    too often . of Oplagt. This is why is actually so in order to
    know the Word, always be continually staring at the Word.

    Visit my homepage: Car Key Programer

  2. Designers will add your book cover Art Ideas text absolutely free.

  3. Designers will certainly include your book cover design programs free text for free.

  4. People feel more comfortable speaking about sensitive topics with someone they meet via the use of a screen. They also
    feel more comfortable communicating with someone who is not in the
    same place as them.

    My web page; Online Psychiatrists

  5. Hello! Do you use Onlyfan twitter?
    I’d like to follow you if that would be ok. I’m undoubtedly enjoying your blog and look forward to new updates.

  6. Here are a few things to keep in mind while searching for the
    best CBD vape juice in your region. The first is to look for full-spectrum CBD.
    This Cbd E Juice Uk vape liquid is created from whole cannabis
    plants, and contains diverse Terpenes.

  7. Mixed fruit vegan 300mg cbd gummies
    Gummies are made from the hemp plant and have no THC, meaning you won’t feel
    any high after taking them. These gummies are loaded with 300 mg of CBD per 100g.

  8. hottest Only fans Models from Onlyfan adore Bella
    Bumzy and Maria Moobs. Sunny and Emmy Beehz are also popular.
    How do you pick the most popular ones? Here are some tips to help you make the right purchase.

  9. It has also been about at this point a friend of mine came into my office and said, “You want this? It’s boring.” He
    threw me a six-cassette audio seminar by Roger Dawson, called Power Negotiating.

    Here is my web page – Key Reprogramming

  10. It is truly a nice Wandsworth Sash Window and door Repairs helpful piece of information. I’m
    happy that you shared this useful info with us. Please stay us informed like
    this. Thank you for sharing.

  11. I got this website from my buddy who informed me
    concerning this site and at the moment this time I am visiting this website and reading very informative
    articles at this time.

    Here is my web site – sash window Refurbishments Wandsworth

  12. work at home表示:

    Hello, i think that i saw you visited my site thus i came to ?return the favor?.I am
    attempting to find things to improve my web site!I suppose its ok to
    use a few of your ideas!!

    Feel free to visit my homepage … work at home

  13. If you are looking to become a psychiatrist but don’t
    have insurance, you could think about Private Psychiatrists practice psychiatry.
    This is a wonderful option and less competition than other fields
    like dermatology or plastic surgery.

  14. joker123.net表示:

    I know this if off topic but I’m looking into starting my own blog and
    was curious what all is required to get setup? I’m assuming having a
    blog like yours would cost a pretty penny? I’m not very web savvy
    so I’m not 100% positive. Any tips or advice would be greatly appreciated.
    Kudos

  15. Definitely, what a great website and enlightening posts,
    I surely will bookmark your website.Best Regards!

    Here is my web site … blackjack games

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

    Feel free to surf to my site – best only fan twitter

  17. Selecting the Best cbd oil Tincture CBD tincture depends on several factors.
    There are Full-spectrum tinctures , Flavorless tincturesas well as THC-free tinctures such
    as Alibi THC-0 tincture oil. To get the most benefits, look for the following qualities:

  18. Cars toting trailers: The runaway truck lesson from the that auto owners hauling trailers should think about, as so.

    my blog Car Door Lock Repair

  19. Robyn表示:

    There are a variety of designs and types of wall mounted electric fire suites; Robyn, mounted electric fires.
    The type of fireplace you choose will determine the dimensions and style.
    They are generally offered in black, though certain models
    are white.

  20. The online application for a payday loan is a viable option for those who need cash but don’t have the money to pay day Loan online off your debts.
    It takes less than five minutes to complete the process.

  21. If you’re looking for the best bunk bed for your child, you might be wondering whether a high sleeper or mid
    sleeper bunk bed is the best choice for your child.

    Visit my blog – midsleeper beds

  22. While you may be tempted to use your favorite payment processor and gateway However, cryptocurrency transactions don’t need them.
    To accept crypto payments (https://Blueberrygaming.in) payments in the same currency, you
    may use a cryptocurrency wallet.

  23. I’m impressed, I have to admit. Rarely do I come
    across a blog that’s both educative and amusing, and let me tell you, you’ve hit
    the nail on the head. The problem is something too
    few men and women are speaking intelligently about. Now i’m very happy I stumbled across
    this during my search for something regarding this.

    my web blog: Sash Window Refurbishments Wandsworth

  24. When your door needs repairs it is recommended to contact an expert upvc door repairs near me repair
    service for your door. They will visit your location and
    provide the products and services you require.

  25. Bert表示:

    Broken windows may mean that you need to replace them, however, it may not be
    immediately obvious. Warped windows are a common indicator that they must be replaced.

    My site; sash window repairs; Bert,

  26. Today, I went to the beach with my kids. I found a sea shell
    and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her
    ear and screamed. There was a hermit crab inside and it
    pinched her ear. She never wants to go back! LoL I know this
    is entirely off topic but I had to tell someone!

    Feel free to surf to my page: windows wandsworth

  27. What’s private mental Health care health?
    It’s a state of mind that is unaffected of negative or harmful behaviors.

    It doesn’t matter whether a person suffers from an illness of
    the mind or a physical condition.

  28. It is crucial to understand On The Wall Fireplace
    features you should look for when buying an electric wall-hung fireplace.

    We’ll cover the Ezee Glow Zara 50” electric fire, the PuraFlame Serena, and the Moda Flame MFE5048WS among others.

  29. When it comes to wardrobe door repairs in aluminium Windows romford There
    are many options. From broken locks to broken doors, there’s a solution for every problem.

    Furniture Mend’s highly trained team can repair all kinds of
    wardrobe doors.

  30. A inexpensive auto locksmiths near me (http://www.sfrpweb.nhely.hu/) locksmith near you
    will be able to assist you if your car is locked and
    cannot be opened. These locksmiths can fix keys that are damaged or lost and can also create
    a duplicate keys for your vehicle and much more.

發佈留言

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