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,480,932 Responses

  1. TravisNOb表示:

    https://mexicanph.com/# mexican rx online
    п»їbest mexican online pharmacies

  2. Heya i am for the first time here. I found this board and I find
    It really helpful & it helped me out a lot. I’m hoping to provide something back and help others like you helped me.

  3. JerryMig表示:

    п»їbest mexican online pharmacies buying prescription drugs in mexico mexican pharmacy

  4. Louishycle表示:

    mexican online pharmacies prescription drugs buying prescription drugs in mexico mexican mail order pharmacies

  5. Having read this I thought it was rather informative.
    I appreciate you finding the time and effort to put this informative
    article together. I once again find myself personally spending way
    too much time both reading and leaving comments.
    But so what, it was still worthwhile!

  6. You have made some good points there. I looked on the net for more
    information about the issue and found most individuals will
    go along with your views on this website.

  7. TravisNOb表示:

    http://mexicanph.shop/# п»їbest mexican online pharmacies
    mexican drugstore online

  8. RandyMix表示:

    Fences look after the needs of heterogeneous purposes, starting from providing safe keeping and retirement to enhancing the aesthetics of a property. In what way, like any nature, fences require maintenance, regular repairs, and now unvaried exchange to certain they carry on with to fulfill their intended functions. In this write-up, we’ll delve into the several services offered close to professional fence companies, including shape, installation, and replacement.

    Wall Mend Services:
    Once again days, fences can suffer abrasion and split satisfactory to brave exposure, aging, or unpremeditated damage. Common issues cover warped or rotted wood, tendency posts, disintegrated panels, lax hardware, and rusted metal components. Maven fence set right services address these issues promptly to reconstruct the non-partisan’s structural integrity and appearance. Skilled technicians assess the damage, persuade suitable repairs, and efficiently move out the requisite commission, whether it involves replacing damaged sections, reinforcing weak spots, or fixing hardware. Past addressing issues at, sidestep put services help string out the lifespan of the fence and ban more national damage.
    get by instatement

    Separate Investiture Services:
    Whether you’re installing a fresh fence in place of insurance, retirement, or aesthetic reasons, official wall installation services certify a hassle-free convert from start to finish. These services typically begin with a consultation to about the client’s needs, preferences, and property specifications. Based on this gen, experts put forward fitting shilly-shally materials, styles, and designs that complement the property’s architecture and chance on the desired functionality. Professional installers treat all aspects of the crowning, from milieu preparation and dispatch context to panel or picket arrangement and closing adjustments. During entrusting the burglary to knowing professionals, property owners can expect a durable, decorously installed circumscribe that enhances contain pray and property value.

    Vacillating Replacement Services:
    When a fence reaches the d‚nouement of its lifespan or sustains irreparable hurt, replacement may be the most applied solution. Vacillating replacement services concern removing the existing unaligned and installing a revitalized ditty tailored to the shopper’s preferences and oddity requirements. Expert cavil companies offer a deviating distribute of fencing materials, including wood, vinyl, aluminum, chain-link, and composite options, allowing property owners to choose the most proper choice based on durability, maintenance requirements, and budget. With fussy acclaim to in depth and status craftsmanship, uncommitted replacement services confirm a seamless transformation to a different, reliable keep that meets the idiosyncrasy’s needs and enhances its appeal.

    Conclusion:
    From repairing minor damages to installing firsthand fences and replacing worn-out structures, experienced fence services rival a crucial role in maintaining the functionality, demeanour, and value of residential and commercial properties. By partnering with experienced professionals, property owners can enjoy peace of positive wise that their fencing needs are in capable hands, ensuring long-lasting, good-looking fences that be in the service of their intended purposes effectively.

  9. Louishycle表示:

    mexican mail order pharmacies medicine in mexico pharmacies best online pharmacies in mexico

  10. JerryMig表示:

    mexico pharmacies prescription drugs mexican rx online mexican drugstore online

  11. Hello! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?

  12. What’s up i am kavin, its my first time to commenting anyplace, when i read this paragraph i thought i could also create comment
    due to this brilliant post.

  13. What’s Taking place i am new to this, I stumbled upon this
    I’ve discovered It absolutely useful and it has helped me out loads.
    I am hoping to contribute & assist different
    customers like its aided me. Good job.

  14. JerryMig表示:

    buying prescription drugs in mexico online buying from online mexican pharmacy mexican border pharmacies shipping to usa

  15. Louishycle表示:

    medication from mexico pharmacy mexican drugstore online mexico drug stores pharmacies

  16. JerryMig表示:

    medication from mexico pharmacy mexico pharmacy mexico drug stores pharmacies

  17. JulioAxowl表示:

    medicine in mexico pharmacies mexico drug stores pharmacies mexican pharmacy

  18. JerryMig表示:

    mexico drug stores pharmacies mexico drug stores pharmacies mexican online pharmacies prescription drugs

  19. JerryMig表示:

    purple pharmacy mexico price list mexican pharmaceuticals online medication from mexico pharmacy

  20. Worman Law LLC
    222 S Meramec Ave Suite 203
    St. Luis MO 63105
    (314)695-9529
    St Louis DWI Lawyers

  21. Louishycle表示:

    best mexican online pharmacies best online pharmacies in mexico medication from mexico pharmacy

  22. RandyMix表示:

    Fences answer for multiple purposes, from providing safe keeping and private to enhancing the beauty of a property. However, like any nature, fences require prolongation, occasional repairs, and now unvaried supplanting to assure they persist to fulfill their intended functions. In this write-up, we’ll delve into the different services offered close to professional restrict companies, including shape, institution, and replacement.

    Non-aligned Repair Services:
    Once again outdated, fences can suffer don and fly proper to unwell exposure, aging, or accidental damage. Common issues encompass warped or rotted wood, leaning posts, docile panels, loose hardware, and rusted metal components. Master fence servicing services address these issues instantaneously to reimburse the vacillating’s structural unity and appearance. Skilled technicians assess the indemnity, persuade proper repairs, and efficiently carry out the of the essence calling, whether it involves replacing damaged sections, reinforcing weak spots, or fixing hardware. Close addressing issues at the crack, uncommitted renewal services improve prolong the lifespan of the independent and intercept more catholic damage.
    enclose replacement

    Vacillate Investiture Services:
    Whether you’re installing a fresh hedge benefit of gage, privacy, or aesthetic reasons, wizard barrier placement services insure a hassle-free process from start to finish. These services typically start off with a consultation to discuss the client’s needs, preferences, and worth specifications. Based on this report, experts advise suitable fence materials, styles, and designs that complement the assets’s architecture and make the acquaintance of the desired functionality. Experienced installers feel all aspects of the installation, from plot preparation and collection setting to panel or picket placement and unchangeable adjustments. Not later than entrusting the area to master professionals, property owners can ahead to a long-wearing, suitably installed circumscribe that enhances check appeal and land value.

    Vacillating Replacement Services:
    When a neutral reaches the put to death of its lifespan or sustains irreparable damage, replacement may be the most efficient solution. Parry replacement services concern removing the existing wall and installing a fashionable one tailored to the customer’s preferences and belongings requirements. Knowledgeable fence companies bid a considerable range of fencing materials, including wood, vinyl, aluminum, chain-link, and composite options, allowing estate owners to settle upon the most applicable option based on durability, maintenance requirements, and budget. With punctilious attention to in depth and quality craftsmanship, uncommitted replacement services ensure a seamless conversion to a restored, responsible keep that meets the worth’s needs and enhances its appeal.

    Conclusion:
    From repairing minor damages to installing virgin fences and replacing worn-out structures, experienced fence services rival a pivotal role in maintaining the functionality, demeanour, and value of residential and commercial properties. Away partnering with sagacious professionals, estate owners can take advantage of peace of do not think twice about knowing that their fencing needs are in talented hands, ensuring long-lasting, drawing fences that serve their intended purposes effectively.

  23. Louishycle表示:

    pharmacies in mexico that ship to usa mexican drugstore online mexican mail order pharmacies

  24. JerryMig表示:

    medicine in mexico pharmacies mexico pharmacy mexican border pharmacies shipping to usa

  25. Louishycle表示:

    purple pharmacy mexico price list buying from online mexican pharmacy buying prescription drugs in mexico online

  26. I don’t even know the way I ended up here, but I thought
    this put up was good. I do not recognise who you might be but definitely you are
    going to a famous blogger should you aren’t already. Cheers!

  27. soccer match表示:

    Hello there! I just wish to offer you a huge thumbs up for the excellent
    information you’ve got here on this post.
    I will be returning to your site for more soon.

  28. Right away I am going to do my breakfast, when having my breakfast coming yet
    again to read further news.

  29. Louishycle表示:

    buying prescription drugs in mexico online mexican mail order pharmacies mexico drug stores pharmacies

發佈留言

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