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,412 Responses

  1. https://drbo8b.s3.us-east-005.backblazeb2.com/research/drbo8b-(194).html
    —are any indication, that adage rings true, particularly when it comes to their own marriage ceremony type.

  2. DominicJaw表示:

    mexican online pharmacies prescription drugs: mexican border pharmacies shipping to usa – medication from mexico pharmacy

  3. DominicJaw表示:

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

  4. Arnoldten表示:

    buying from online mexican pharmacy п»їbest mexican online pharmacies best online pharmacies in mexico

  5. DominicJaw表示:

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

  6. Arnoldten表示:

    mexican pharmacy buying from online mexican pharmacy mexican drugstore online

  7. These are genuinely great ideas in regarding blogging. You have touched some good points here. Any way keep up wrinting.

  8. When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time
    a comment is added I get three e-mails with the same comment.
    Is there any way you can remove me from that service?
    Many thanks!

  9. https://ya-drbogin-2.us-ord-1.linodeobjects.com/massage-and-its-impact-on-chronic-pain-management.html
    With cap sleeves and an illusion neckline, this fitted blue magnificence was perfect for this D.C.

  10. lottery表示:

    Aw, this was an incredibly nice post. Taking the time
    and actual effort to generate a very good article… but what can I say… I put things off a lot and never
    seem to get anything done.

  11. Nelsonbluck表示:

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

  12. What’s The Most Creative Thing Happening With Adult Toys men Adult toys

  13. WayneCoirl表示:

    mexican pharmaceuticals online: best online pharmacies in mexico – medication from mexico pharmacy

  14. 99811760.xyz表示:

    20 Things You Should Have To Ask About Spare Car Key Cost Before You Buy Spare Car Key Cost 99811760.xyz

  15. DominicJaw表示:

    mexican pharmaceuticals online: mexican mail order pharmacies – buying from online mexican pharmacy

  16. Nelsonbluck表示:

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

  17. DominicJaw表示:

    mexican mail order pharmacies: mexican pharmaceuticals online – mexico drug stores pharmacies

  18. Trefkja表示:

    Добрый день!
    Как избежать рисков при покупке диплома колледжа или ПТУ в России
    mipropuestadenegocio.com/category/uncategorized/page/3
    Поможем вам всегда!.

  19. Isaevclub.Ru表示:

    Don’t Buy Into These “Trends” About Private Psychiatric Assessment Glasgow psychiatric assessment brighton (Isaevclub.Ru)

  20. Nelsonbluck表示:

    purple pharmacy mexico price list: buying prescription drugs in mexico – buying prescription drugs in mexico online

  21. try this site表示:

    Hey There. I discovered your blog using msn. This is a really smartly written article.
    I will make sure to bookmark it and come back to learn more of your helpful information.
    Thanks for the post. I will definitely return.

  22. 5 Laws To Help The Retro White Fridge Freezer Industry side by side fridge freezer for RVs

  23. Couple toys表示:

    Guide To Best Couple Toys: The Intermediate Guide To Best Couple Toys Couple toys

  24. https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/ya-drbogin-10/the-role-of-massage-in-supporting-immune-system-health.html
    To make hers, mother JoJo Cohen turned to her close good friend, the late designer L’Wren Scott.

發佈留言

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