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,155,133 Responses

  1. 비아그라 성분
    비아그라의 주요 성분 및 작용 원리

  2. toptan poset表示:

    Remarkable! Its really remarkable paragraph, I have got much clear idea regarding from this post.

  3. Hayallerinizdeki kumarhane Sweet bonanza

  4. You Can Explain Best ADHD Medication For Adults To
    Your Mom strattera Adhd medication (http://www.pickyourown.org)

  5. Elvis表示:

    Why You’re Failing At Slot Machine Games slot games (Elvis)

  6. nft hit the headlines thanks to the amazing https://wafflestoken.com/ sales, in this regard it’s worth to find out more in relation to, what is it – nft.

  7. This Is The Intermediate Guide Towards Replacement
    Upvc Window Handles Replacement glass in windows

  8. Duane表示:

    15 Reasons Not To Be Ignoring Online Shopping Sites Uk online shopping sites with
    free international shipping (Duane)

  9. Medicines information sheet. Drug Class.
    where to buy cheap colchicine without rx
    Some what you want to know about medicine. Get information now.

  10. Larry表示:

    Shopping Online: What No One Is Talking About shopping online sites,
    Larry,

  11. www.google.ng表示:

    5 Killer Quora Answers To Online Home Shop Uk Discount Code Online home shop Uk
    discount code – http://www.google.ng,

  12. The 10 Most Scariest Things About Bipolar Psychiatrist Near Me Bipolar Psychiatrist Near Me

  13. Asbestos Law表示:

    Although Asbestos Law is still banned numerous legislative proposals for incremental
    legislation have been tossed around Congress.
    One of these, the Frank R.

  14. Condenser表示:

    In Scarborough, Maine, having access to reliable electrical
    services is crucial due to the town’s growing residential and commercial
    infrastructure. LaPlante Electric provides essential services such as electrical wiring, circuit breaker repair,
    and lighting installation. Serving neighborhoods like Oak Hill and Pine Point,
    LaPlante Electric ensures that residents and businesses have dependable electrical systems to handle everything from routine maintenance to complex installations.

    The importance of having a skilled electrician in Scarborough cannot be overstated, as proper electrical maintenance and prompt repairs are vital for ensuring safety and functionality in homes
    and workplaces.

    LaPlante Electric is located in Scarborough,
    a town founded in 1658. Scarborough has developed into a thriving community with a population of 20,352 as of 2018 and 9,168 households.
    The town is conveniently connected by major highways, including State Route
    207, which facilitates easy travel across the region. An interesting fact about Scarborough is its rich history and scenic landscapes, making it a desirable place to live and visit.
    The town offers a blend of residential, commercial, and recreational opportunities, contributing to its charm
    and appeal.

    The cost of electrical repairs in Scarborough can vary depending on the service required.
    Basic services like outlet repair or lighting installation might
    range from $100 to $300, while more extensive work such as electrical panel upgrades or home rewiring can cost
    between $500 and $3,000. Scarborough experiences
    a range of temperatures, with summer highs reaching around 80°F and winter lows dropping to approximately 15°F.

    These temperature fluctuations make it essential for residents to have reliable electrical systems that
    can handle both heating and cooling needs, ensuring year-round comfort and safety.

    Scarborough offers numerous points of interest
    that cater to a variety of tastes and preferences.
    Blue Point Preserve is a beautiful natural area perfect for
    outdoor activities and bird watching. Bug Light Park provides stunning views of the harbor and is a popular
    spot for picnics and kite flying. Crescent Beach State Park offers a pristine shoreline for
    swimming and sunbathing. The Eastern Trail is a scenic route for biking and hiking, connecting various parts
    of Scarborough. Lastly, Scarborough Beach State Park is known for its surf and sand,
    attracting visitors for its recreational opportunities.
    Each of these attractions provides unique experiences
    that highlight Scarborough’s diverse cultural
    and natural offerings.

    Choosing LaPlante Electric for electrical services in Scarborough is a wise decision for
    residents and businesses seeking reliable and efficient solutions.
    The company offers a comprehensive range of services, including electrical troubleshooting, surge
    protection, and smart home installation. LaPlante Electric’s commitment to quality workmanship and exceptional customer service
    ensures that all electrical needs are met promptly and professionally.
    For those living in Scarborough, LaPlante Electric is the trusted partner for maintaining a functional and safe electrical
    system, providing peace of mind and comfort throughout the year.

    In Scarborough, Maine, maintaining reliable electrical systems is essential due to the town’s expanding infrastructure and
    diverse weather conditions. LaPlante Electric provides
    crucial services such as electrical wiring, circuit breaker repair, and lighting installation.
    Serving neighborhoods like Bendale and Blue Point, LaPlante Electric
    ensures that both residential and commercial clients have dependable
    electrical systems to handle various needs. The
    significance of having a skilled electrician in Scarborough cannot be overstated, as
    proper electrical maintenance and timely repairs are
    vital for ensuring safety and functionality in homes and businesses.

    LaPlante Electric operates in Scarborough, a town founded in 1658.
    Scarborough has grown into a vibrant community with a population of 20,
    352 as of 2018 and 9,168 households. The town is well-connected by major highways,
    including State Route 207, which facilitates easy
    travel across the region. An interesting fact about Scarborough is its combination of
    historical charm and modern amenities, making it an attractive place to
    live and work. The town offers a mix of residential, commercial, and recreational opportunities, contributing to its unique character and appeal.

    Electrical repair costs in Scarborough can vary widely depending on the type of
    service required. Basic services like outlet repair or lighting installation might range from $100 to
    $300, while more extensive work such as electrical
    panel upgrades or home rewiring can cost between $500 and $3,000.
    Scarborough experiences a range of temperatures, with summer highs
    reaching around 80°F and winter lows dropping to approximately 15°F.
    These temperature variations necessitate reliable electrical systems that can handle both heating and cooling demands, ensuring
    year-round comfort and safety for residents.

    Scarborough offers numerous points of interest that cater to a variety of preferences.
    Blue Point Preserve is a beautiful natural area perfect
    for outdoor activities and bird watching. Bug Light
    Park provides stunning views of the harbor and is a popular spot for picnics and kite flying.
    Crescent Beach State Park offers a pristine shoreline for
    swimming and sunbathing. The Eastern Trail is a scenic route for biking and hiking, connecting various parts of Scarborough.
    Lastly, Scarborough Beach State Park is known for its surf and sand, attracting visitors for its recreational opportunities.
    Each of these attractions offers unique experiences that highlight Scarborough’s diverse cultural and natural offerings.

    Choosing LaPlante Electric for electrical services in Scarborough is a wise decision for residents and businesses
    seeking reliable and efficient solutions. The company offers a comprehensive range of services, including electrical troubleshooting, surge protection, and smart home installation. LaPlante Electric’s commitment to quality workmanship
    and exceptional customer service ensures that all electrical needs are met promptly and professionally.
    For those living in Scarborough, LaPlante Electric is the trusted partner for
    maintaining a functional and safe electrical system,
    providing peace of mind and comfort throughout the
    year.

    In Scarborough, Maine, reliable electrical services are crucial due to the town’s growing residential and commercial infrastructure.
    LaPlante Electric provides essential services such as electrical
    wiring, circuit breaker repair, and lighting installation. Serving neighborhoods
    like Cliffside and Eight Corners, LaPlante Electric ensures that residents and businesses have dependable electrical systems to handle everything
    from routine maintenance to complex installations. The importance of having a
    skilled electrician in Scarborough cannot be overstated, as
    proper electrical maintenance and prompt repairs are vital for ensuring safety and functionality in homes
    and workplaces.

    LaPlante Electric is located in Scarborough, a town founded
    in 1658. Scarborough has developed into a thriving community with a population of
    20,352 as of 2018 and 9,168 households. The town is conveniently connected by major highways, including State Route 207, which facilitates easy travel across the region. An interesting fact about Scarborough is
    its rich history and scenic landscapes, making it a desirable place to live
    and visit. The town offers a blend of residential, commercial, and recreational opportunities, contributing to its charm and appeal.

    The cost of electrical repairs in Scarborough can vary depending on the service required.
    Basic services like outlet repair or lighting installation might range
    from $100 to $300, while more extensive work
    such as electrical panel upgrades or home rewiring can cost between $500 and $3,000.
    Scarborough experiences a range of temperatures, with summer highs reaching around 80°F and winter
    lows dropping to approximately 15°F. These temperature fluctuations make it essential for residents to have reliable
    electrical systems that can handle both heating and cooling needs, ensuring year-round comfort and
    safety.

    Scarborough offers numerous points of interest that cater to
    a variety of tastes and preferences. Blue
    Point Preserve is a beautiful natural area perfect for outdoor activities and bird watching.
    Bug Light Park provides stunning views of the harbor and is a popular spot for picnics and kite flying.

    Crescent Beach State Park offers a pristine shoreline for swimming and
    sunbathing. The Eastern Trail is a scenic route for
    biking and hiking, connecting various parts of Scarborough.

    Lastly, Scarborough Beach State Park is known for its surf and
    sand, attracting visitors for its recreational opportunities.

    Each of these attractions provides unique experiences that highlight Scarborough’s diverse cultural and natural offerings.

    Choosing LaPlante Electric for electrical services in Scarborough is a
    wise decision for residents and businesses seeking reliable and efficient solutions.
    The company offers a comprehensive range of services,
    including electrical troubleshooting, surge protection, and smart
    home installation. LaPlante Electric’s commitment to quality workmanship and exceptional customer service ensures that all electrical needs are met
    promptly and professionally. For those living in Scarborough, LaPlante
    Electric is the trusted partner for maintaining a functional and safe electrical system,
    providing peace of mind and comfort throughout the year.

    In Scarborough, Maine, maintaining reliable electrical systems is essential due to the town’s expanding infrastructure and diverse weather conditions.
    LaPlante Electric provides crucial services such as electrical wiring,
    circuit breaker repair, and lighting installation. Serving neighborhoods like Guildwood and Highland Creek,
    LaPlante Electric ensures that both residential and commercial clients have dependable electrical systems to
    handle various needs. The significance of having a skilled
    electrician in Scarborough cannot be overstated, as proper electrical maintenance
    and timely repairs are vital for ensuring safety and functionality in homes and businesses.

    LaPlante Electric operates in Scarborough, a town founded in 1658.
    Scarborough has grown into a vibrant community with a population of 20,352 as of 2018 and 9,168 households.
    The town is well-connected by major highways, including State Route 207, which facilitates easy
    travel across the region. An interesting fact about
    Scarborough is its combination of historical charm and modern amenities, making it an attractive
    place to live and work. The town offers a
    mix of residential, commercial, and recreational opportunities, contributing to its unique character and appeal.

  15. Responsible For A Adhd Assessment Adult Budget?
    10 Terrible Ways To Spend Your Money gp Adhd Assessment

  16. Thank you for the auspicious writeup. It in reality was once a amusement account it.

    Look advanced to more added agreeable from you! By the way, how can we keep in touch?

  17. cassylawn表示:

    10 Wrong Answers To Common Asbestos Exposure Mesothelioma
    Questions Do You Know The Right Answers? cassylawn

  18. You could certainly see your expertise within the article you write.

    The sector hopes for even more passionate writers like you who are not afraid
    to mention how they believe. Always go after your heart.

  19. Auto表示:

    See What Auto Accident Lawsuit Tricks The Celebs Are Making Use Of Auto

  20. 15 Things Your Boss Would Like You To Know You Knew About Birth Injury Attorneys Birth injury lawyer

  21. The 10 Most Scariest Things About Upvc Repairs Near Me upvc repairs near me

  22. You’ll Be Unable To Guess Window Repair Near Me’s Tricks Window Repair near me

  23. Asbestos lawsuits are complicated as they are often involving
    multiple jurisdictions. Law firms with experience in handling asbestos claims at both the state
    and national levels understand the unique laws involved.

    my blog post – Mesothelioma Compensation

發佈留言

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