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,210,945 Responses

  1. Stephaine表示:

    Why You Should Focus On Making Improvements To Mazda
    Replacement Keys 2011 mazda 3 key fob – Stephaine,

  2. Nelsonbluck表示:

    mexican border pharmacies shipping to usa: purple pharmacy mexico price list – buying from online mexican pharmacy

  3. 登記工商表示:

    https://accounting1016.s3.us-west-004.backblazeb2.com/research/accounting1016-(41).html
    Encourage your mother to have somewhat enjoyable when dressing for your ceremony or rehearsal dinner.

  4. https://tendoncare25.b-cdn.net/research/tendoncare25-(242).html
    The straps and sleeves you select in your dress will have an result on the neckline and shape of your costume.

  5. The Three Greatest Moments In Tree House Twin Bunk Bed History
    Treehouse bed for girl

  6. Josephsinia表示:

    Medicine guide available. Medicine brochure provided.
    medco online pharmacy
    Current drug trends. Interactions explained here.

  7. Hi there! Do you know if they make any plugins to
    help with SEO? I’m trying to get my blog to rank for some targeted keywords but I’m not
    seeing very good gains. If you know of any please share.
    Kudos!

  8. Arnoldten表示:

    buying from online mexican pharmacy п»їbest mexican online pharmacies mexican rx online

  9. Are You Tired Of Subaru Keys? 10 Inspirational Sources That Will
    Bring Back Your Passion replacing a Subaru key

  10. Arnoldten表示:

    mexico pharmacies prescription drugs pharmacies in mexico that ship to usa mexican mail order pharmacies

  11. Drugs prescribing information. What side effects?
    buying cheap cipro tablets
    All about medicines. Read information now.

  12. Arnoldten表示:

    mexican online pharmacies prescription drugs mexican border pharmacies shipping to usa mexico pharmacies prescription drugs

  13. DominicJaw表示:

    mexico pharmacies prescription drugs: reputable mexican pharmacies online – mexican mail order pharmacies

  14. The Reasons To Focus On Making Improvements In Window Doctor
    London bifold door repair london

  15. What Do You Do To Know If You’re Ready For Replace Toyota Car Key toyota aygo replacement key uk

  16. 整復師表示:

    https://tendoncare7.s3.us-east-005.backblazeb2.com/research/tendoncare7-(412).html
    Go for prints that talk to your marriage ceremony location, and most importantly, her personal fashion.

  17. DominicJaw表示:

    pharmacies in mexico that ship to usa: purple pharmacy mexico price list – mexico drug stores pharmacies

  18. Pretty great post. I simply stumbled upon your weblog and wanted
    to mention that I have really enjoyed surfing around your
    blog posts. In any case I will be subscribing on your rss feed and I hope you write again soon!

  19. 台北 整復表示:

    https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/bogin37/research/bogin37-(102).html
    This glittery lace-knit two-piece features a sleeveless cocktail costume and coordinating longline jacket.

  20. Nelsonbluck表示:

    reputable mexican pharmacies online: mexico drug stores pharmacies – mexican online pharmacies prescription drugs

  21. DominicJaw表示:

    medicine in mexico pharmacies: mexico drug stores pharmacies – mexican border pharmacies shipping to usa

  22. Bristol Window Repair: What Nobody Is Talking About Upvc Window Repairs

  23. 10 Ways To Create Your American Fridgefreezer Empire American fridge freezer 90Cm wide

  24. Arnoldten表示:

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

  25. https://drbo6b.z12.web.core.windows.net/research/drbo6b-(26).html
    Our moms are attractive and wonderful and they should rock what they have.

  26. DominicJaw表示:

    buying prescription drugs in mexico online: mexican pharmaceuticals online – reputable mexican pharmacies online

  27. Nelsonbluck表示:

    mexican online pharmacies prescription drugs: п»їbest mexican online pharmacies – mexico pharmacies prescription drugs

  28. DominicJaw表示:

    mexican online pharmacies prescription drugs: medicine in mexico pharmacies – medication from mexico pharmacy

  29. I have really learned some new things by your website. One other thing I’d prefer to say is always that newer laptop os’s are likely to allow far more memory to get used, but they in addition demand more memory space simply to function. If your computer can’t handle more memory plus the newest software program requires that ram increase, it may be the time to shop for a new Laptop. Thanks

  30. WayneCoirl表示:

    medicine in mexico pharmacies: mexican pharmaceuticals online – mexico drug stores pharmacies

發佈留言

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