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,211,093 Responses

  1. 15 Amazing Facts About Best Male Masturbation Device
    best Male masterbaiters

  2. https://digi5.b-cdn.net/research/digi5-(170).html
    To allow you to in your search, we’ve carried out the hard work for you!

  3. Nelsonbluck表示:

    buying prescription drugs in mexico online: pharmacies in mexico that ship to usa – mexico drug stores pharmacies

  4. 按摩學徒表示:

    https://tendoncare22.b-cdn.net/research/tendoncare22-(450).html
    If you are trying to splurge on a MOB costume, you will find loads of glam choices here.

  5. Double Glazing Repairs Near Me Tools To Streamline Your Everyday Lifethe Only Double Glazing Repairs Near Me Trick Every Individual
    Should Be Able To double glazing Repairs near me

  6. injury表示:

    9 Lessons Your Parents Taught You About Injury Lawsuit injury

  7. The Hidden Secrets Of Fridge Freezer 50/50 Frost Free Best 50 50 Integrated Fridge Freezer

  8. macauslot88表示:

    This text is priceless. How can I find out more?

  9. Nelsonbluck表示:

    п»їbest mexican online pharmacies: mexico drug stores pharmacies – mexican rx online

  10. Arnoldten表示:

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

  11. turcja表示:

    I was suggested this website by means of mmy cousin. I’m no longer
    positive whether or not this publish is written by him as nobody else realize succh particular about mmy problem.

    You are amazing! Thank you!

    Feel free to surf to mmy site – turcja

  12. https://ya-tendoncare-4.sos-at-vie-1.exo.io/tutorial-on-pulling-tendons-how-to-pull-tendons-to-increase-physical-strength-3.html
    You should bear in mind the formality, theme, and decor colour of the wedding while looking for the gown.

  13. The 9 Things Your Parents Teach You About Vauxhall Corsa Key Fob Replacement vauxhall corsa Key fob replacement

  14. 11 Methods To Totally Defeat Your Car Locksmiths Elsycrays.Top [https://Www.Elsycrays.Top/]

  15. 台中 整骨表示:

    https://bogin020.netlify.app/research/bogin020-(28)
    Karen Kane has beautiful choices that look slightly more informal if you’re not in search of a full robe.

  16. evisa vietnam表示:

    I need to to thank you for this excellent read!!
    I certainly loved every little bit of it. I have got you book marked to check out new stuff you post…

  17. DominicJaw表示:

    п»їbest mexican online pharmacies: buying from online mexican pharmacy – mexican drugstore online

  18. WayneCoirl表示:

    mexican rx online: best online pharmacies in mexico – pharmacies in mexico that ship to usa

  19. 新竹 整骨表示:

    http://bogin21.s3-website-us-east-1.amazonaws.com/research/bogin21-(29).html
    I think every of the above may work with vineyard weddings.

  20. https://accounting27.blr1.digitaloceanspaces.com/research/accounting27-(203).html
    “She bought it on a whim and ended up winning,” the bride stated.

  21. Personal Injury Lawyers Tools To Help You Manage Your Daily Lifethe One Personal Injury Lawyers Trick That
    Every Person Should Know Personal injury Lawyers

  22. 5 Common Phrases About Auto Accident Legal You Should Stay Clear Of auto accident attorney

  23. Arnoldten表示:

    medicine in mexico pharmacies buying prescription drugs in mexico online purple pharmacy mexico price list

  24. Nelsonbluck表示:

    mexican online pharmacies prescription drugs: mexican pharmaceuticals online – mexico pharmacies prescription drugs

發佈留言

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