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,372,127 Responses

  1. Stevenrip表示:

    legit canadian pharmacy: Large Selection of Medications from Canada – reputable canadian pharmacy

  2. Marcelabelp表示:

    http://canadaph24.pro/# my canadian pharmacy rx

  3. Rickysem表示:

    indian pharmacy online buy medicines from India world pharmacy india

  4. Sofia表示:

    This Is A Mesothelioma Lawsuits Success Story You’ll Never Be
    Able To mesothelioma litigation (Sofia)

  5. 7 Little Changes That’ll Make The Difference With Your Designer Handbags Brown designer handbags sale outlet uk

  6. Rickysem表示:

    mexico drug stores pharmacies Mexican Pharmacy Online pharmacies in mexico that ship to usa

  7. Rickysem表示:

    canadianpharmacyworld canadian pharmacies canadian pharmacy online reviews

  8. We are a group of volunteers and opening a brand new scheme in our community.
    Your site offered us with valuable information to work on. You’ve
    done a formidable process and our whole neighborhood shall be grateful to you.

  9. Rickysem表示:

    indian pharmacy online indianpharmacy com india pharmacy mail order

  10. Your Family Will Thank You For Having This Replacement Double Glazed Glass Only Near Me replace double glazed window

  11. Why You Should Focus On Making Improvements In Planet Spa Aromatherapy Calm Planet Spa Aromatherapy Calm

  12. Jonathan表示:

    10 Tips To Know About Double Glazing Near Me repairs to double glazing windows (Jonathan)

  13. Double doors表示:

    Meet Your Fellow Double Glazing Fitters Near Me Enthusiasts.
    Steve Jobs Of The Double Glazing Fitters Near Me Industry
    Double doors

  14. Rickysem表示:

    buy canadian drugs Licensed Canadian Pharmacy drugs from canada

  15. Courtney表示:

    What’s The Reason You’re Failing At Replacement Glass For Windows window
    replacement – Courtney,

  16. Pandemic or not, girls of color—indeed, all workers—deserve to be valued and compensated pretty.

    Also visit my homepage: https://integramais.com.br/2024/04/18/the-importance-of-finding-the-right-job-dress-code/

  17. You’ll Never Guess This Sofa Couch For Sale’s Secrets Sofa Couch For Sale

  18. JustinCit表示:

    mail order pharmacy india http://indiaph24.store/# Online medicine order
    cheapest online pharmacy india

  19. A Step-By-Step Guide To Folding Scooters foldable Mobility scooter

  20. Michaelcew表示:

    https://indiaph24.store/# india online pharmacy

  21. 5 Reasons Double Glazing Companies Near Me Is A Good Thing replacement double Glazed glass

  22. Marcelabelp表示:

    https://mexicoph24.life/# medication from mexico pharmacy

  23. The Reasons You Should Experience Ford Key Fob At
    A Minimum, Once In Your Lifetime ford Van key replacement (http://www.0471tc.com)

  24. JustinCit表示:

    reputable indian pharmacies http://indiaph24.store/# reputable indian online pharmacy
    buy medicines online in india

  25. Pedroereva表示:

    mexican pharmaceuticals online: mexican pharmacy – п»їbest mexican online pharmacies

  26. Cindy表示:

    15 Up-And-Coming Can You Earn CSGO Cases Bloggers You Need To Keep An Eye On case falchion [Cindy]

  27. Pansy表示:

    I’m not sure where you’re getting your information, but good topic.
    I needs to spend some time learning more or understanding more.
    Thanks for fantastic information I was looking for this information for my mission.

  28. Pedroereva表示:

    mexican mail order pharmacies: mexican pharmacy – mexican pharmaceuticals online

  29. 10 Things We All Do Not Like About Motor Vehicle Compensation Motor Vehicle Accident Lawyers

  30. Michaelcew表示:

    http://canadaph24.pro/# legal to buy prescription drugs from canada

發佈留言

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