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

1,631,299 Responses

  1. I think the admin of this web page is truly working hard in favor of his web page,
    for the reason that here every stuff is quality based information.

    Also visit my web site น้ำปลาร้า ส้มตำ

  2. Are You In Search Of Inspiration? Check Out Replacement Window Handle replacement Window handle

  3. 5 Killer Queora Answers On Window Handles Replacement window handles replacement

  4. Fantastic website you’ve got here.. It�s difficult to find high quality writing similar yours at the present time. I honestly appreciate people similar you! Bear care!!

    Also visit my blog: http://Forum-digitalna.nb.rs/viewtopic.php?f=8&t=95096

  5. Davydov_zsEl表示:

    Эдуард Давыдов http://eduarddavydovbsk.ru/ .

  6. My progrɑmmer is tгying to convince me to move to .net fom
    PHP. I have alwas disⅼiked the idea because of the expenses.
    But hе’s tryiong none the less. I’ve Ƅeen using Moᴠable-type onn
    a variety of ᴡebsіtes for abоut a ʏear and am woгried about switching to another platform.
    I have heard veгy goopd things about blοgengine.net.
    Is there a wayy I can transfer alⅼ myy wordpress content ino it?
    Any kind of help would bee rеally appreсiated!

    my blog – kunjungi situs ini

  7. JamesCof表示:

    lasix: Over The Counter Lasix – lasix side effects

  8. Harriet表示:

    How Do I Explain Replacement Double Glazed Windows To A
    Five-Year-Old window replacement cost – Harriet,

  9. RonaldBatty表示:

    The Complete Manual to Gutter Setting up, Rain Gutter Guard Installation, and Gutter Cleaning Services
    If you are an owner of a house, one grasp the significance of maintaining a efficient rain gutter system. Gutters play a vital function in protecting your house from water damage by directing rainwater away from the roof and base. However, ensuring your rain gutters are in optimal shape demands more than just periodic cleaning. From rain gutter installation to rain gutter guard installation and gutter cleansing services, this complete manual covers everything one require to know to keep your rain gutters in optimal condition.

    Rain Gutter Setting up
    Why Rain Gutters Are Important
    Before we plunge into rain gutter setting up, let’s comprehend why gutters are crucial for your home. Rain gutters help stop water harm by directing rainwater away from your house’s foundation, walls, and landscaping. Without proper drainage, rainwater runoff can lead to erosion, basement flooding, mold growth, and structural harm.

    Signs You Require New Rain Gutters
    Cracks or Rust: Visible cracks or rust spots indicate that your gutters are deteriorating and may need to be replaced.
    Sagging or Pulling Away: Rain gutters that sag or pull away from the home are not effectively directing water away and ought to be replaced.
    Water Damage: Water stains on the exterior of your house or basement flooding may suggest that your gutters are failing to do their job.
    Frequent Clogs: If your rain gutters are continuously clogged with debris, it may be time to invest in a new gutter system that is easier to maintain.

    Rain Gutter Materials
    When it comes to gutter materials, one have several options to choose from:

    Aluminum: Lightweight, rust-resistant, and available in a variety of colors.
    Vinyl: Affordable, easy to set up, and low maintenance.
    Steel: Durable and suitable for regions with harsh weather conditions.
    Copper: Elegant, long-lasting, and adds decorative flair to your house.

    DIY vs. Professional Installation
    While some homeowners may try to set up rain gutters themselves, it’s often preferable to leave this job to the professionals. Professional gutter installers have the experience, tools, and expertise to ensure that your rain gutters are installed correctly and operate optimally. Plus, hiring a professional can save you time and hassle in the long run.

    Rain Gutter Guard Setting up
    The Importance of Rain Gutter Guards
    Rain Gutter guards are an crucial addition to any rain gutter system. These protective covers prevent leaves, twigs, and debris from clogging your gutters, reducing the requirement for regular cleaning and minimizing the risk of water damage to your house.

    Types of Rain Gutter Guards
    Mesh Screens: Fine mesh screens allow water to flow through while blocking debris.
    Reverse Curve: These gutter guards feature a curved surface that directs water into the gutter while preventing leaves and debris from entering.
    Brush Inserts: Brush-style inserts fit inside the rain gutter and prevent leaves from accumulating while allowing water to flow freely.
    Foam Inserts: Foam rain gutter guards block debris while allowing water to filter through.

    Professional Setting up vs. DIY
    While some house owners may attempt to install rain gutter guards themselves, professional installation is often recommended. Professional installers can ensure that gutter guards are properly fitted and securely attached, reducing the risk of debris buildup and water damage.

    Gutter Cleansing Services
    The Importance of Rain Gutter Cleaning
    Regular rain gutter cleaning is essential for maintaining a functional rain gutter system. Over time, leaves, twigs, and debris can accumulate in your rain gutters, resulting to clogs and water overflow. Professional gutter cleaning assistance can help avert these issues and extend the life of your gutters.

    Signs You Need Rain Gutter Cleaning
    Visible Debris: If you observe leaves, twigs, or other debris in your rain gutters, it’s time for a cleansing.
    Water Overflow: Overflowing gutters during rainstorms suggest that they are clogged and require to be cleaned.
    Sagging Rain Gutters: Rain gutters that sag or pull away from the home may be weighed down by debris and need cleaning.
    Gutter cleaning service

    Frequency of Rain Gutter Cleaning
    The frequency of gutter cleaning depends on several factors, including the surrounding vegetation, climate, and rain gutter guard installation. In general, it’s recommended to clean your rain gutters at least twice a year, ideally in the spring and fall, to remove seasonal debris buildup.

    Conclusion
    Maintaining a functional gutter system is essential for protecting your home from water damage and preserving its structural integrity. Whether you need rain gutter installation, rain gutter guard installation, or gutter cleaning services, investing in professional assistance can save you time, money, and hassle in the long run. By prioritizing gutter, you can ensure that your home remains safe, dry, and secured for years to come.

  10. Profile表示:

    Fastidious replies in return of this matter with solid arguments and telling all regarding that.

  11. 12 Stats About Repair Upvc Windows To Make You Think About
    The Other People Window doctor near me

  12. 125.141.133.9表示:

    The Top Double Glazing Repairs Near Me That Gurus Use Three Things double Glazing repairs near me (125.141.133.9)

  13. Sale表示:

    15 Things You’ve Never Known About Full Spectrum Cbd
    Oil Uk Sale

  14. 10 Quick Tips About Window Sash Replacement Window sash replacement

  15. 5 Killer Quora Answers To Double Glazing Repairs Near Me Double Glazing

  16. At this time it looks like BlogEngine is the preferred blogging platform out there right now.
    (from what I’ve read) Is that what you are using on your blog?

  17. Stephenpem表示:

    https://stromectol.fun/# ivermectin stromectol

  18. Stephenpem表示:

    https://lisinopril.top/# lisinopril 40

  19. crypto.com表示:

    Aw, this was an incredibly nice post. Spending some time and actual effort to
    generate a very good article… but what can I say… I procrastinate a
    whole lot and never manage to get anything done.

  20. LeonardFaulk表示:

    amoxicillin 500 mg capsule: order amoxicillin online uk – buy amoxicillin 500mg uk

  21. JamesCof表示:

    ivermectin pill cost: stromectol tab – ivermectin cost canada

  22. JamesCof表示:

    50mg lisinopril: zestoretic 20 25mg – ordering lisinopril without a prescription

  23. RonaldBatty表示:

    The Complete Guide to Gutter Setting up, Gutter Protector Setting up, and Rain Gutter Cleansing Assistance
    If you’re a homeowner, you understand the value of maintaining a efficient rain gutter setup. Gutters play a vital role in shielding your home from water damage by channeling water from rainfall away from the roof and base. However, ensuring your rain gutters are in top condition demands more than just periodic cleansing. From gutter setting up to rain gutter guard installation and gutter cleansing services, this complete guide covers everything you require to know to keep your rain gutters in optimal shape.

    Gutter Installation
    Why Rain Gutters Are Important
    Before we plunge into gutter installation, let’s grasp why gutters are essential for your house. Gutters help prevent rainwater damage by directing rainwater away from your house’s base, walls, and landscaping. Without adequate water drainage, rainwater runoff can cause wear and tear, basement flooding, mold growth, and constructional damage.

    Signs You Need New Gutters
    Cracks or Rust: Visible cracks or rust spots indicate that your gutters are deteriorating and may need to be replaced.
    Sagging or Pulling Away: Rain gutters that sag or pull away from the home are not efficiently directing rainwater away and should be replaced.
    Water Damage: Water stains on the exterior of your house or basement flooding may indicate that your rain gutters are failing to do their job.
    Frequent Clogs: If your gutters are constantly clogged with debris, it may be time to invest in a new rain gutter system that is easier to maintain.

    Gutter Materials
    When it comes to rain gutter materials, you have various options to choose from:

    Aluminum: Lightweight, rust-resistant, and accessible in a variety of colors.
    Vinyl: Affordable, easy to set up, and low maintenance.
    Steel: Durable and appropriate for regions with harsh weather conditions.
    Copper: Elegant, long-lasting, and adds decorative flair to your house.

    DIY vs. Professional Installation
    While some homeowners might try to set up rain gutters themselves, it’s often best to leave this job to the professionals. Professional rain gutter installers have the experience, tools, and expertise to ensure that your gutters are installed correctly and operate optimally. Plus, hiring a professional can save you time and hassle in the long run.

    Rain Gutter Guard Installation
    The Importance of Gutter Guards
    Rain Gutter guards are an crucial addition to any rain gutter system. These protective covers prevent leaves, twigs, and debris from clogging your rain gutters, reducing the requirement for regular cleaning and reducing the risk of water damage to your home.

    Types of Gutter Guards
    Mesh Screens: Fine mesh screens allow water to flow through while blocking debris.
    Reverse Curve: These rain gutter guards feature a curved surface that directs water into the gutter while stopping leaves and debris from entering.
    Brush Inserts: Brush-style inserts fit inside the rain gutter and prevent leaves from accumulating while allowing water to flow freely.
    Foam Inserts: Foam gutter guards block debris while allowing water to filter through.

    Professional Setting up vs. DIY
    While some homeowners may attempt to set up gutter guards themselves, professional setting up is often recommended. Professional installers can ensure that gutter guards are properly fitted and securely attached, minimizing the risk of debris buildup and water damage.

    Rain Gutter Cleansing Assistance
    The Importance of Rain Gutter Cleaning
    Regular rain gutter cleaning is essential for maintaining a functional gutter system. Over time, leaves, twigs, and debris can accumulate in your rain gutters, resulting to clogs and water overflow. Professional rain gutter cleaning services can help prevent these issues and extend the life of your gutters.

    Signs You Need Rain Gutter Cleaning
    Visible Debris: If you observe leaves, twigs, or other debris in your gutters, it’s time for an cleansing.
    Water Overflow: Overflowing rain gutters during rainstorms suggest that they are clogged and require to be cleaned.
    Sagging Gutters: Gutters that sag or pull away from the house may be weighed down by debris and require cleaning.
    Gutter cleaning service

    Frequency of Gutter Cleaning
    The frequency of rain gutter cleaning depends on several factors, including the surrounding vegetation, climate, and gutter guard installation. In general, it’s recommended to clean your gutters at least twice a year, ideally in the spring and fall, to remove seasonal debris buildup.

    Conclusion
    Maintaining a functional rain gutter system is essential for safeguarding your home from water damage and preserving its structural integrity. Whether you need gutter installation, gutter guard installation, or rain gutter cleaning assistance, investing in professional assistance can save you time, money, and hassle in the long run. By prioritizing gutter, you can ensure that your house remains safe, dry, and protected for years to come.

  24. Regan表示:

    The Best Tips You’ll Receive About Glass Window Repair pvc window repairs (Regan)

  25. Excellent blog right here! Also your site a lot up fast!
    What web host are you using? Can I am getting your associate hyperlink for your host?
    I wish my site loaded up as fast as yours lol

  26. The Most Effective Double Glazing Companies Near
    Me Tips To Rewrite Your Life double glazing companies near Me

  27. Greetings. Make it me set out by revealing you the author’s term – Wilhemina Valenti and furthermore she free of charge digs that name.
    My family lives in Puerto Rico. Hiring is how I make a living.
    His wife doesn’t like this task the means by which he genuinely does but the actions he significantly likes doing is work outs but he
    is struggling to learn time as it. Go to her internet site to seek out more: mega зеркало

  28. JamesCof表示:

    generic prednisone otc: where to buy prednisone in canada – order prednisone online no prescription

發佈回覆給「Double Glazing」的留言 取消回覆

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