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,429,757 Responses

  1. Williamagric表示:

    prescription drugs online without https://pharm24.pro/# ed meds online

  2. There Is No Doubt That You Require Peritoneal Mesothelioma Asbestos Mesothelioma Attorneys

  3. Medicine prescribing information. What side effects?
    buy inderal tablets
    All trends of medicament. Get now.

  4. BrandonSaisy表示:

    indian pharmacies safe: Indian pharmacy to USA – Online medicine home delivery

  5. Williamagric表示:

    ed natural treatment https://pharm24.pro/# ed cures that actually work

  6. Jorgeagopy表示:

    reputable indian online pharmacy: Best online Indian pharmacy – indian pharmacies safe

  7. LeroyBak表示:

    mexican online pharmacies prescription drugs Legit online Mexican pharmacy medication from mexico pharmacy

  8. Jere表示:

    A reliable law firm that is specialized in mesothelioma can aid families and victims in receiving compensation. Mesothelioma lawyers understand asbestos case – Jere, litigation and can aid victims with trust
    fund claims, lawsuits, or VA benefits.

  9. How To Get More Value Out Of Your Folding Treadmills With Incline Treadmill foldable

  10. ufiy.com表示:

    Why Electric Fireplace Wall Mounted Is Harder
    Than You Imagine fireplaces shop [ufiy.com]

  11. 11 “Faux Pas” You’re Actually Able To Create Using Your Retro Fridge Freezer 50 50 Space-Saving Design Side By Side Fridge Freezer

  12. LeroyBak表示:

    mexican drugstore online mexican pharm 24 mexican pharmaceuticals online

  13. Hey are using WordPress for your blog platform? I’m new to the blog world
    but I’m trying to get started and create my own. Do you need any html coding knowledge to make your own blog?
    Any help would be really appreciated!

  14. Diplomi_rzoi表示:

    куплю аттестат камень [url=https://prema-diploms.ru/]prema-diploms.ru[/url] .

  15. I am truly glad to read this website posts which
    contains lots of helpful facts, thanks for
    providing these kinds of data.

  16. whoah thiѕ blog is wonderful i love reading youг posts.

    Keep up the good work! You alгeady know, lots оf people ɑre hunting rоund for tһis info, yoou can heⅼp them greatly.

    Havee а looҝ at my webpage; Cuantoto Wap Login

  17. At this time it seems like Expression Engine is the best
    blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

  18. LeroyBak表示:

    online shopping pharmacy india medicines from India best online pharmacy india

  19. DevonPhack表示:

    https://pharm24.pro/# best cure for ed
    buy prescription drugs without doctor

  20. Jorgeagopy表示:

    canadian online drugs: low cost pharmacy – male erectile dysfunction

  21. Jorgeagopy表示:

    drugs causing ed: low cost pharmacy – viagra without a doctor prescription walmart

  22. LeroyBak表示:

    pharmacy drugs cheap drugs natural ed treatment

  23. The Most Innovative Things That Are Happening With Semi Truck Accident truck accident attorneynear
    me (http://49.51.81.43)

  24. Williamagric表示:

    prescription drugs online without doctor http://pharm24.pro/# how to help ed

  25. 7 Secrets About Online Mystery Box That No One Will Tell You mystery box opening

  26. For many years, I have actually battled unpredictable blood sugar level swings that left
    me feeling drained and inactive. But considering that integrating
    Sugar my energy levels are currently stable and regular, and I no more strike a wall in the mid-days.
    I value that it’s a gentle, natural strategy that does not come with any
    type of unpleasant negative effects. It’s genuinely transformed my every day life.

  27. Thanks for one’s marvelous posting! I certainly enjoyed reading it,
    you are a great author.I will be sure to bookmark your blog and may come back in the future.
    I want to encourage yourself to continue your great job, have a nice morning!

發佈留言

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